0

I'm trying to create a shared library which in turn uses libtorch and libngt. libtorch provides a proper way to link the project to itself and that works fine but somehow I'm running into issue while linking libngt using cmake.

My CMakeLists.txt

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(blksim_test)

find_package(Torch REQUIRED)
link_directories("/usr/local/lib/")
add_executable(test_sim test_sim.cc hash_network.cc hash_pool.cc)
target_include_directories(test_sim PRIVATE ../include)
target_link_libraries(test_sim "${TORCH_LIBRARIES}")
target_link_libraries(test_sim ngt)
set_property(TARGET test_sim PROPERTY CXX_STANDARD 14 )

I run into these errors:

Error:
/usr/bin/ld: CMakeFiles/test_sim.dir/hash_pool.cc.o: in function `NGT::Index::open(std::string const&, bool)':
hash_pool.cc:(.text._ZN3NGT5Index4openERKSsb[_ZN3NGT5Index4openERKSsb]+0x2e): undefined reference to `NGT::Index::open(std::string const&, bool, bool)'
/usr/bin/ld: CMakeFiles/test_sim.dir/hash_pool.cc.o: in function `NGT::Index::createGraphAndTree(std::string const&, NGT::Property&, bool)':
hash_pool.cc:(.text._ZN3NGT5Index18createGraphAndTreeERKSsRNS_8PropertyEb[_ZN3NGT5Index18createGraphAndTreeERKSsRNS_8PropertyEb]+0x6c): undefined reference to `NGT::Index::createGraphAndTree(std::string const&, NGT::Property&, std::string const&, unsigned long, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/test_sim.dir/build.make:133: test_sim] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test_sim.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

I made sure to export the PATH and LD_LIBRARY_PATH to include /usr/local/lib where libngt is located.

instance1:~/code$ ls -al /usr/local/lib/libngt.*
-rw-r--r-- 1 root root 4037082 Mar 20 21:40 /usr/local/lib/libngt.a
lrwxrwxrwx 1 root root      11 Mar 20 23:02 /usr/local/lib/libngt.so -> libngt.so.1
lrwxrwxrwx 1 root root      16 Mar 20 23:02 /usr/local/lib/libngt.so.1 -> libngt.so.1.14.5
-rw-r--r-- 1 root root 1581560 Mar 20 21:41 /usr/local/lib/libngt.so.1.14.5

I can't figure out what am I doing wrong. I'm using cmake for the first time so I'm almost certain that something is wrong with the way I'm linking libngt.

I tried creating a FindNGT.cmake in the build directory but even that didn't help. I don't know what am I missing.

The Dreams Wind
  • 8,416
  • 2
  • 19
  • 49
  • ***tried creating a FindNGT.cmake in the build directory but even that didn't help.*** You would then need a find_package(ngt REQUIRED) for CMake to use it. – drescherjm Mar 23 '23 at 20:07
  • I tried find_package() as well(in CMakeLists.txt), along with FindNGT.cmake but that did not solve the issue. – vishal saraswat Mar 23 '23 at 20:20
  • You would have to show your `FindNGT.cmake` – drescherjm Mar 23 '23 at 20:21
  • You may want to use target_link_directories instead of link_directories. The documentation here urges you to do that: [https://cmake.org/cmake/help/latest/command/link_directories.html](https://cmake.org/cmake/help/latest/command/link_directories.html) – drescherjm Mar 23 '23 at 20:23
  • 2
    @drescherjm: The code in the question post actually links with `ngt` library: `target_link_libraries(test_sim ngt)`. But it looks like the library doesn't define the symbols which it normally should... vishal saraswat: You could print content of the library using `nm -gDC /usr/local/lib/libngt.so.1.14.5` and check, whether the library actually contains those symbols. – Tsyvarev Mar 23 '23 at 20:33
  • Thanks for helping me out. :) I tried the target_link_directories() suggestion by changing the CMakeLists.txt file to this. cmake_minimum_required(VERSION 3.0 FATAL_ERROR) project(blksim_test) find_package(Torch REQUIRED) add_executable(test_sim hash_network.cc hash_pool.cc test_sim.cc) target_include_directories(test_sim PUBLIC ../include) target_link_libraries(test_sim "${TORCH_LIBRARIES}") target_link_directories(test_sim PRIVATE /usr/local/lib/) target_link_libraries(test_sim ngt) set_property(TARGET test_sim PROPERTY CXX_STANDARD 14) Though that did not change anything. – vishal saraswat Mar 23 '23 at 20:36
  • @Tsyvarev thanks for that suggestion. I did check that already and well, I did build a sample cpp file with just libngt and that worked fine. instance1:~/code$ nm -gDC /usr/local/lib/libngt.so.1.14.5 | grep Index::open 00000000000a89d0 T NGT::Index::open(std::__cxx11::basic_string, std::allocator > const&, bool, bool) 00000000000fb770 W NGTQ::Index::open(std::__cxx11::basic_string, std::allocator > const&, bool) – vishal saraswat Mar 23 '23 at 20:38
  • 1
    That `__cxx11::` namespace, which you can observe in `nm` output, seems to be a problem: Your library is built with different setting of `_GLIBCXX_USE_CXX11_ABI` than your CMake project. See e.g. [that question](https://stackoverflow.com/questions/55406770/gcc-undefined-references-with-abicxx11) about this setting in CMake. – Tsyvarev Mar 23 '23 at 20:45
  • Thanks a bunch, @Tsyvarev. :) That solved the issue. I learned something new today. :) – vishal saraswat Mar 23 '23 at 21:38

0 Answers0