Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake linking to shared library cannot find library

On 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 then copy my library file libCamera.so into the build directory. After running make, the main.cpp.o file compiles successfully, but I receive the following error during linking:

/usr/bin/ld: cannot find -lCamera

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

like image 703
Karnivaurus Avatar asked Jul 14 '15 17:07

Karnivaurus


People also ask

How do I add a library to CMake project?

To add a library in CMake, use the add_library() command and specify which source files should make up the library. Rather than placing all of the source files in one directory, we can organize our project with one or more subdirectories. In this case, we will create a subdirectory specifically for our library.

What is Link_directories?

link_directories([AFTER|BEFORE] directory1 [directory2 ...]) Adds the paths in which the linker should search for libraries. Relative paths given to this command are interpreted as relative to the current source directory, see CMP0015 . The command will apply only to targets created after it is called.

How does CMake Find_library work?

find_library tells CMake that we're looking for a library and once we have found it, to store the path to it in the variable LIBIMAGEPIPELINE_LIBRARY . Likely filenames are passed with NAMES LibImagePipeline . It is good practice to pass the names without the file extension like .


1 Answers

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

target_link_libraries(Test Camera)

if your library not found you may need to add directory, where 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 dll files stored with executables, in Linux that is not the case, so it would be /usr/lib, not /usr/bin. Also you may change LD_LIBRARY_PATH variable to make your program to find a library in a custom location.

like image 180
Slava Avatar answered Sep 28 '22 17:09

Slava