cmake - find_library 或 link_directories 或 find_package?什么是更好的方法?错误 - 使用 cmake 链接库

标签 cmake gstreamer dynamic-linking shared-libraries

给定
文件 /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_packagefind_library 即可。

至少这样,如果未找到依赖项,您的项目将在 CMake 配置时而不是链接时失败。

关于cmake - find_library 或 link_directories 或 find_package?什么是更好的方法?错误 - 使用 cmake 链接库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857000/

相关文章:

c++: 未定义的错误引用

Gstreamer MP4 中缺少音频

rust - 我如何确定为什么已编译的 Rust 二进制文件需要特定符号?

c++ - 链接到旧版本的 gcc

c - 从 appsink 解析数据时出现内部数据流错误

cmake - 将GLEW与CMake链接

c++ - 如何在 cmake >= 2.6 中使用没有 gnu 扩展的 c++98?

cmake - 使用 emscripten 时如何解析 freetype 头文件

c - 我如何告诉 cmake 执行这两个步骤来使用 winpcap?

c - GstMultifilesink 后消息回调