[CMake] What is recommended to export / import target libraries with dependencies that are also exported

Fabien Spindler Fabien.Spindler at inria.fr
Wed Nov 5 02:38:55 EST 2014


Hello,

Consider the following case where:
- I want to export/import my library named mylib
- This library depends on OpenCV that is also exported

According to 
http://www.cmake.org/cmake/help/v3.0/manual/cmake-packages.7.html#creating-packages
the CMakeLists.txt of my library is simpilar to:

project(mylib)
find_package(OpenCV)
add_library(mylib foo.c foo.h)
target_link_libraries(mylib ${OpenCV_LIBS})
...
export(EXPORT mylibTargets FILE 
"${CMAKE_CURRENT_BINARY_DIR}/mylibTargets.cmake")
...

Now the CMakeLists of an other project that uses mylib will be like :

project(test)
find_package(mylib)
add_executable(test test.c)
target_link_libraries(test mylib)

During the build of this project the linker complains that OpenCV 
libraries are not found

Linking C executable test
"/Applications/CMake 3.0.0.app/Contents/bin/cmake" -E cmake_link_script 
CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/cc   -Wl,-search_paths_first -Wl,-headerpad_max_install_names   
CMakeFiles/test.dir/test.c.o  -o test -lopencv_videostab -lopencv_video ...
ld: library not found for -lopencv_videostab

The problem comes from mylibTargets.cmake where I have:

set_target_properties(mylib PROPERTIES
   IMPORTED_LINK_INTERFACE_LIBRARIES_NOCONFIG 
"opencv_videostab;opencv_video;..."
   IMPORTED_LOCATION_NOCONFIG "/tmp/build/libmylib.dylib"
   IMPORTED_SONAME_NOCONFIG "/tmp/build/libmylib.3.dylib"
   )

while I wanted to have the full path to OpenCV libraries (ie 
/tmp/OpenCV/lib/libopencv_videostab.2.4.10.dylib ...

Is there a proper way to include OpenCV dependencies during the export 
of my library ?

Fabien


More information about the CMake mailing list