find_library或link_directories或find_package?什么是更好的方法?错误 - 使用cmake链接库 [英] find_library or link_directories or find_package? What is better way? Error - Link libraries using cmake
问题描述
给定
存在 /usr/lib/gstreamer-0.10/libgstffmpeg.so
文件
在CMakeLists.txt中进行更改
Given
The file /usr/lib/gstreamer-0.10/libgstffmpeg.so
is present
Making changes in CMakeLists.txt
方法1 find_library()
find_library(GST_FFMPEG NAMES gstffmpeg PATHS /usr/lib/gstreamer-0.10/)
...
target_link_libraries(MyLibraryOrMyExecutable $ {GST_FFMPEG})
find_library(GST_FFMPEG NAMES gstffmpeg PATHS /usr/lib/gstreamer-0.10/ )
...
target_link_libraries( MyLibraryOrMyExecutable ${GST_FFMPEG} )
当我运行 make
与上面的配置(方法1),我得到以下错误
When I run make
with above configuration(approach 1), I get the following errors
/ bin / ld:warning: libvpx。 so1 ,需要/usr/lib/i386-linux-gnu/libavcodec.so.53,找不到(尝试使用-rpath或-rpath-link)
/ bin / ld:warning: libschroedinger-1.0.so.0 ,需要/usr/lib/i386-linux-gnu/libavcodec.so.53,找不到(尝试使用-rpath或-rpath-link )
/ bin / ld:warning: libgsm.so.1 ,需要/usr/lib/i386-linux-gnu/libavcodec.so.53,找不到请尝试使用-rpath或-rpath-link)
/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)
看起来添加的库取决于更多的库,我可以看到上面3个.so文件在/ usr / lib。因此,方法1的一个可能的解决方案可能是添加三个 find_library()
函数。
Looks like the added library depends on more libraries which are not linked! I can see above 3 .so files in /usr/lib. So one possible solution to approach 1 could be to add three more find_library()
functions. right ?
可能不是 - 探讨了在上面可能的解决方案中发现的问题
May be not- This question explores the issues found in above possible solution
- Q1。有没有其他方法,可以节省查找
依赖库和链接它们的努力?
方法2 link_directories()
link_directories(/usr/lib/gstreamer-0.10/)
target_link_libraries(MyLibraryOrMyExecutable gstffmpeg)
link_directories(/usr/lib/gstreamer-0.10/)
target_link_libraries( MyLibraryOrMyExecutable gstffmpeg )
当我运行 make
方法2),我收到以下错误
When I run make
with above configuration(approach 2), I get the following errors
bin / ld:找不到-lgstffmpeg
bin/ld: cannot find -lgstffmpeg
- Q2。如何使用上述方法解决问题2?
- Q3。哪种方法1或2更好?
试图阅读文档的cmake和搜索的SO,但无法解决我的问题。
我尝试了两种方法,并且都有问题
P.S. Tried reading documentation for cmake and searching on SO but could not resolve my problem. I tried two approaches and have issues with both them
推荐答案
是方法1.
从 link_directories
:
请注意,此命令很少需要。 find_package()和find_library()返回的库位置是绝对路径。将这些绝对库文件路径直接传递给target_link_libraries()命令。 CMake将确保链接器找到它们。
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
。
Regardless of the approach you take, I don't know of an easy way to automatically get the list of these "sub-dependencies" and add them. I'd just do a find_package
or find_library
for each.
至少这样,如果没有找到依赖项,您的项目将在CMake配置时间而不是链接时失败。
At least that way, if a dependency isn't found, your project fails at CMake configure time rather than at link time.
这篇关于find_library或link_directories或find_package?什么是更好的方法?错误 - 使用cmake链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!