7

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...

Karnivaurus
  • 22,823
  • 57
  • 147
  • 247

1 Answers1

4

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.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • If I do that, then I get the same error message as before: `Linking CXX executable Test`, `/usr/bin/ld: cannot find -lCamera` – Karnivaurus Jul 15 '15 at 14:02
  • I have added the line `link_directories(/home/karnivaurus/Projects/Test/build)` to my CMakeLists.txt file, but I still get the same error.... However, if I copy the library to `/usr/lib`, as you suggested, it compiles fine.... Any idea why the first attempt did not solve it? – Karnivaurus Jul 15 '15 at 14:29
  • @Karnivaurus btw for `build` you should use cmake var as mentioned above, but full path should still work. Why it does not is difficult to say without looking into actual CMakeLists.txt – Slava Jul 15 '15 at 14:34
  • @Karnivaurus note that binary is being buit and linked in build/src subdirectory. If you put lib there you may not need to add that dir. But normally you should not copy library there but use `find_library()` instead – Slava Jul 15 '15 at 14:38