问题
Given
The file /usr/lib/gstreamer-0.10/libgstffmpeg.so
is present
Making changes in CMakeLists.txt
Approach 1 find_library()
find_library(GST_FFMPEG NAMES gstffmpeg PATHS /usr/lib/gstreamer-0.10/ )
...
target_link_libraries( MyLibraryOrMyExecutable ${GST_FFMPEG} )
When I run make
with above configuration(approach 1), I get the following errors
/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)
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. Is there any other method which saves the effort of finding the dependent libraries and linking them ? An approach using which all the dependent libraries get automatically linked ?
Approach 2 link_directories()
link_directories(/usr/lib/gstreamer-0.10/)
target_link_libraries( MyLibraryOrMyExecutable gstffmpeg )
When I run make
with above configuration(approach 2), I get the following errors
bin/ld: cannot find -lgstffmpeg
- Q2. How to solve the issue with above approach 2?
- Q3. Which approach 1 or 2 is better ?
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:
To answer your Q3 first, I think the preferred method is approach 1.
From the docs for 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.
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.
At least that way, if a dependency isn't found, your project fails at CMake configure time rather than at link time.
回答2:
I encounter similar scenario, I solve it by add in CMakesList.txt, just before find_library(), add this command:
set(CMAKE_PREFIX_PATH /the/path/to/your/lib/)
,but normally if your dependencies libraries are in usr/lib or usr/local/lib paths, it could find by well called the functions.
来源:https://stackoverflow.com/questions/17857000/find-library-or-link-directories-or-find-package-what-is-better-way-error-li