0

I want to use a library called CompactNSearch in my project but the compilation throws an error which i have not been able to fix for hours.

Setup for MWE

Directory, clone the library with git clone https://github.com/InteractiveComputerGraphics/CompactNSearch.git:

├── CMakeLists.txt
├── CompactNSearch 
...
└── main.cpp

Main.cpp

#include "CompactNSearch.h"
using namespace CompactNSearch;

int main()
{
    float r = 0;
    NeighborhoodSearch nsearch(r);
}

CMakeList.txt

cmake_minimum_required(VERSION 3.22)

project(MyProject VERSION 1.0
                  LANGUAGES CXX)

add_executable(
    MyProject main.cpp
)

add_subdirectory(CompactNSearch)
target_link_libraries(MyProject CompactNSearch)

What i'm doing

mkdir build
cd build
cmake ..
make

What happens during make

[ 16%] Building CXX object CompactNSearch/CMakeFiles/CompactNSearch.dir/src/CompactNSearch.cpp.o
[ 33%] Linking CXX static library libCompactNSearch.a
[ 33%] Built target CompactNSearch
[ 50%] Building CXX object CMakeFiles/MyProject.dir/main.cpp.o
[ 66%] Linking CXX executable MyProject
/usr/bin/ld: CMakeFiles/MyProject.dir/main.cpp.o: in function `main':
main.cpp:(.text+0x44): undefined reference to `CompactNSearch::NeighborhoodSearch::NeighborhoodSearch(float, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/MyProject.dir/build.make:100: MyProject] Error 1
make[1]: *** [CMakeFiles/Makefile2:117: CMakeFiles/MyProject.dir/all] Error 2
make: *** [Makefile:136: all] Error 2

What i've tried so far

Basically any variant of linking the library with cmake that i could find - i've also tried building it as static or shared in its folder and linking that with the same result. I added a #error This should be shown in the constructor definition and it definitely gets compiled.

I would be very grateful for any pointers (hah) that help me - not just for this project but also to generally understand CMake better.

SOLUTION:

Turns out the library used a compile flag to decide if float or double should be used and the default did not match my usage. So i added set(USE_DOUBLE_PRECISION OFF) in CMakeList.txt before the subdirectory is added and now everything is just peachy. Thanks everyone and hopefully this can help others, even if the problem was very specific to the library i want to use :)


Hakka
  • 1
  • 1
  • Run `nm -C | grep NeighborhoodSearch` where file is the library name – Something Something Oct 07 '22 at 15:05
  • @HenriqueBucher thanks for the recommendation, did not know that tool! I pasted the output here: https://pastebin.com/N8vNfeFh Seems to me that everything is in order. The function as a signature (float, bool) but the bool has a default value and it doesn't change a thing if i also pass a bool. – Hakka Oct 07 '22 at 15:22
  • Run make with `make VERBOSE=1` and paste the command line here – Something Something Oct 07 '22 at 15:35
  • 1
    The only 2 lines containing constructors in the code are 2 lines containing `0000000000000080 T CompactNSearch::NeighborhoodSearch::NeighborhoodSearch(double, bool)`. Your code expects `NeighborhoodSearch(float, bool)` though. A look at the relevant header shows a conditionally defined type alias `Real` being used as first constructor parameter. Probably relevant compile definition wasn't added via via `target_compile_definitions(CompactNSearch PUBLIC USE_DOUBLE)` but with private visibility instead. – fabian Oct 07 '22 at 15:40
  • https://github.com/InteractiveComputerGraphics/CompactNSearch/blob/0865da3aeeb25e09b0bed4185b6f2ffb3e557909/include/Config.h#L5 – fabian Oct 07 '22 at 15:40
  • Thanks Fabian, i guess after dealing with other compile errors for half the day with the larger project i did not not see this (in hindsight obvious) mismatch. It works now, i'll amend my original post. You saved me a lot of further headaches :) – Hakka Oct 07 '22 at 15:50

0 Answers0