Cmake链接到共享库无法找到库 [英] Cmake linking to shared library cannot find library
问题描述
在Ubuntu上,我有两个目录: build
和 src
。在 src
中,我的 CMakeLists.txt
文件具有以下行:
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)
在构建目录( cmake ../ src
)中运行cmake后,然后复制我的库文件 libCamera.so
导入构建目录。运行make后, main.cpp.o
文件成功编译,但在连接期间收到以下错误:
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
为什么是这样?共享库在我正在建立的同一个目录...和同样的事情发生,如果我将该库复制到 / usr / bin
...
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
...
推荐答案
您不应该将前缀 lib
和后缀。所以
的库,所以只需使用:
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
注意:您将lib复制到 / usr / bin
但不像Windows中的dll文件与可执行文件存储,在Linux中不是这样,所以它会是 / usr / lib
,而不是 / usr / bin
。此外,您可以更改 LD_LIBRARY_PATH
变量,使您的程序在自定义位置中查找库。
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.
这篇关于Cmake链接到共享库无法找到库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!