user2618142
user2618142

Reputation: 1065

find_library or link_directories or find_package? What is better way? Error - Link libraries using cmake

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

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

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

Upvotes: 3

Views: 6666

Answers (2)

vivi
vivi

Reputation: 334

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.

Upvotes: 0

Fraser
Fraser

Reputation: 78418

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.

Upvotes: 1

Related Questions