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 :)