Cmake链接到共享库无法找到库

时间:2022-09-18 12:25:55

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

在Ubuntu上,我有两个目录:build和src。在src,我CMakeLists。txt文件有行:

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:

在构建目录(cmake ./src)中运行cmake之后,我将复制我的库文件libCamera。进入构建目录。在运行后,main.cpp。o文件编译成功,但链接时收到以下错误:

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

这是为什么呢?共享库位于我正在构建的目录中……如果我把库复制到/usr/ bin…

1 个解决方案

#1


4  

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

您不应该在库中添加前缀lib和后缀。

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.

注意:您将lib复制到/usr/bin,但是不像Windows中dll文件存储在可执行文件中,在Linux中不是这样的,所以应该是/usr/lib,而不是/usr/ bintable。还可以更改LD_LIBRARY_PATH变量,使程序在自定义位置找到一个库。

#1


4  

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

您不应该在库中添加前缀lib和后缀。

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.

注意:您将lib复制到/usr/bin,但是不像Windows中dll文件存储在可执行文件中,在Linux中不是这样的,所以应该是/usr/lib,而不是/usr/ bintable。还可以更改LD_LIBRARY_PATH变量,使程序在自定义位置找到一个库。