给定
文件 /usr/lib/gstreamer-0.10/libgstffmpeg.so
存在
在 CMakeLists.txt 中进行更改
方法 1 find_library()
find_library(GST_FFMPEG NAMES gstffmpeg PATHS /usr/lib/gstreamer-0.10/ )
...
target_link_libraries( MyLibraryOrMyExecutable ${GST_FFMPEG} )
当我使用上述配置(方法 1)运行 make
时,出现以下错误
/bin/ld: warning: libvpx.so.1, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libschroedinger-1.0.so.0, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
/bin/ld: warning: libgsm.so.1, needed by /usr/lib/i386-linux-gnu/libavcodec.so.53, not found (try using -rpath or -rpath-link)
看起来添加的库依赖于更多未链接的库!我可以在/usr/lib中看到以上3个.so文件。因此,方法 1 的一种可能解决方案是再添加三个 find_library()
函数。对吗?
可能不是-This question探讨上述可能解决方案中发现的问题
- Q1。有没有其他方法可以省去寻找的麻烦 依赖库并链接它们?使用一种方法自动链接所有依赖库?
方法 2 link_directories()
link_directories(/usr/lib/gstreamer-0.10/)
target_link_libraries( MyLibraryOrMyExecutable gstffmpeg )
当我使用上述配置(方法 2)运行 make
时,出现以下错误
bin/ld: cannot find -lgstffmpeg
- Q2。上述方法2如何解决问题?
- Q3。方法 1 和方法 2 哪个更好?
附注尝试阅读 cmake 文档并搜索 SO 但无法解决我的问题。 我尝试了两种方法,但都遇到了问题
最佳答案
首先回答你的问题3,我认为首选方法是方法1。
来自 link_directories
的文档:
Note that this command is rarely necessary. Library locations returned by find_package() and find_library() are absolute paths. Pass these absolute library file paths directly to the target_link_libraries() command. CMake will ensure the linker finds them.
无论您采用哪种方法,我都不知道有什么简单的方法可以自动获取这些“子依赖项”的列表并添加它们。我只需为每个执行一个 find_package
或 find_library
即可。
至少这样,如果未找到依赖项,您的项目将在 CMake 配置时而不是链接时失败。
关于cmake - find_library 或 link_directories 或 find_package?什么是更好的方法?错误 - 使用 cmake 链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857000/