Cmake links to shared library cannot find library

In Ubuntu, I have two directories: build and src . In src my CMakeLists.txt file has the lines:

 add_executable(Test main.cpp) target_link_libraries(Test libCamera.so) 

After running cmake in the build directory ( cmake ../src ), I will then libCamera.so library libCamera.so to the build directory. After running make, the main.cpp.o file compiles successfully, but when linking, I get the following error:

 /usr/bin/ld: cannot find -lCamera 

Why is this? The shared library is in the same directory I create ... and the same thing happens if I copy the library to /usr/bin ...

+5
source share
1 answer

You should not put the lib prefix and the .so suffix in the library, so just use:

 target_link_libraries(Test Camera) 

If your library is not found, you may need to add the directory where the library is located:

 link_directories( /home/user/blah ) # for specific path link_directories( ${CMAKE_CURRENT_BINARY_DIR} ) # if you put library where binary is generated 

Note. You copied lib to /usr/bin , but unlike Windows, where the DLL files stored in executables are not the case on Linux, so it will be /usr/lib , not /usr/bin . You can also change the LD_LIBRARY_PATH variable so that your program finds the library in a custom location.

+4
source

All Articles