Open3D icon indicating copy to clipboard operation
Open3D copied to clipboard

Open3D cmake package confuses the linker

Open ghost opened this issue 6 years ago • 4 comments

IMPORTANT: Please use the following template to report the bug.


Describe the bug Linking to Open3D::Open3D confuses the linker. Given a certain combination of libraries, it complains about undefined references.

To Reproduce I built Open3D from source according to the docs. It is installed system wide. It does not matter whether it is built as static or shared.

I am able to reproduce the issue with a simple project. There are four files with dummy contents:

lib.h
lib.cpp
main.cpp
CMakelists.txt

With the following contents:

// lib.h
#include <string>
void foo(std::string bar);
// lib.cpp
#include "lib.h"
void foo(std::string bar) {}
// main.cpp
#include "lib.h"
int main(int argc, char **argv) {
    foo("");
    return 0;
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
project(o3debug)
set(CMAKE_CXX_STANDARD 17)

find_package(Open3D)

add_library(lib SHARED lib.cpp)

add_executable(main main.cpp)
target_link_libraries(main lib Open3D::Open3D)

Note that the only use of Open3D is during linkage. Assuming that one is next to these four files, do the following to reproduce the error:

mkdir build && cd build
cmake ..
make
Scanning dependencies of target lib
[ 25%] Building CXX object CMakeFiles/lib.dir/lib.cpp.o
[ 50%] Linking CXX shared library liblib.so
[ 50%] Built target lib
Scanning dependencies of target main
[ 75%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: CMakeFiles/main.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x4e): undefined reference to `foo(std::string)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:105: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:124: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:103: all] Error 2

Removing the Open3D::Open3D from the target_link_libraries resolves the issue. Swapping the link order does not help.

Interestingly, getting rid of #include<string> and changing bar to int from std::string also resolves the issue.

Thus I concluded that I have no idea what is going on and turned here for help :)

Expected behavior I expect the project to build normally.

Environment (please complete the following information):

  • Operating system: Ubuntu20
  • Python version: (bindings disabled)
  • Open3D version: 0.11.2
  • Is this remote workstation?: no
  • How did you install Open3D?: source
  • Compiler version (if built from source): gcc9.3, tried 8.0 as well

Additional info For completeness, here's the output of cmake

-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Deprecation Warning at /usr/local/lib/cmake/Open3D/Open3DConfig.cmake:18 (cmake_policy):
  The OLD behavior for policy CMP0072 will be removed from a future version
  of CMake.

  The cmake-policies(7) manual explains that the OLD behaviors of all
  policies are deprecated and that a policy should be set to OLD only under
  specific short-term circumstances.  Projects should be ported to the NEW
  behavior and not rely on setting a policy to OLD.
Call Stack (most recent call first):
  CMakeLists.txt:5 (find_package)


-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Found Open3D: /usr/local/lib/cmake/Open3D/Open3DConfig.cmake (found version "0.11.2") 
-- Configuring done
-- Generating done
-- Build files have been written to: /somewhere/o3debug/build

ghost avatar Nov 24 '20 11:11 ghost

@TobiFisch you can check this script to include Open3D into a C++ project. https://github.com/intel-isl/Open3D/blob/master/docs/_static/C++/CMakeLists.txt

sanskar107 avatar Dec 01 '20 15:12 sanskar107

Right, that was my reference. As you can see, the CMakeLists.txt I posted is basically equal to that reference, just without the WIN32 switches. Or did I miss anything? Can you reproduce the issue on your machine?

ghost avatar Dec 01 '20 15:12 ghost

I think this is the same issue as this one: https://github.com/intel-isl/Open3D/issues/2286

Jason1820 avatar Dec 04 '20 10:12 Jason1820

Closing since #2286 has been resolved. The new CXX11 ABI is used by default, so you can link to system libraries without changing the cmake defaults.

ssheorey avatar Feb 04 '22 04:02 ssheorey