CMake:链接共享库 [英] Cmake: linking shared library
问题描述
我已经做过几次了,但是这次我还是被卡住了.我有一个可执行文件"myapp"和一个自己的共享库"mylib".在我的cmakelist中,我有以下内容:
I've done this before a couple of times, but somehow I'm stuck this time. I have an executable "myapp" and a own shared library "mylib". In my cmakelists I have the following:
ADD_LIBRARY(mylib SHARED ${SOURCES_LIB})
INSTALL(TARGETS mylib DESTINATION .)
ADD_EXECUTABLE(myapp ${SOURCES_APP})
TARGET_LINK_LIBRARIES(myapp ${QT_LIBRARIES} mylib)
INSTALL(TARGETS myapp DESTINATION .)
所有内容都能正确编译和链接,但是当我启动myapp时,出现以下错误:
Everything compiles and links correctly, but when I start myapp, I get the following error:
error while loading shared libraries: libmylib.so: cannot open shared object file: No such file or directory
lib和可执行文件位于安装目录中.当我通过将上述cmakelists的第一行更改为以下内容来使我的图书馆成为静态图书馆时:
The lib and the executable are present in the install directory. When I make my library static by changing the first line of the above cmakelists to:
ADD_LIBRARY(mylib STATIC ${SOURCES_LIB})
然后一切正常工作100%.
then everything works 100%.
有人知道我在做什么吗?
Does anyone know what I'm doing wrong?
推荐答案
在安装库和可执行文件期间,会从可执行文件中删除用于查找库的运行时路径.因此,您的库必须驻留在运行时库搜索路径中.例如,在Linux下,启动可执行文件时,尝试将LD_LIBRARY_PATH设置为包含已安装库的目录.
During the installation of your library and executable, the runtime paths to find the library are stripped from the executable. Therefore your library has to reside in the runtime library search path. For example under Linux, try to set LD_LIBRARY_PATH to the directory that contains the installed library when starting your executable.
这篇关于CMake:链接共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!