我在C ++中使用CMake构建一个依赖于dlib
的共享库。
虽然我可以使用dlib
和make
构建和安装共享的make install
,但到目前为止,我还没有找到链接到此的方法共享dlib
库。
DLib网站中给出的使用示例始终链接到静态库。
这是我到目前为止所做的:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
find_package(dlib)
add_library(face_align SHARED src/mylib.cpp)
target_link_libraries(face_align dlib::dlib)
链接器抱怨如下:
/usr/bin/ld: cannot find -ldlib::dlib
collect2: error: ld returned 1 exit status
CMakeFiles/face_align.dir/build.make:94: recipe for target 'libface_align.so' failed
make[2]: *** [libface_align.so] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/face_align.dir/all' failed
make[1]: *** [CMakeFiles/face_align.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
如果我从SHARED
删除了
add_library(face_align SHARED src/mylib.cpp)
项目构建成功。这让我觉得dlib:dlib
指向静态库。我看到有一个dlib:dlib_shared
,但没有成功链接到这个。
答案 0 :(得分:2)
答案似乎是(1)链接到dlib作为静态库(2)使代码链接到dlib 位置无关:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
# Unfortunately, this only links dlib as static library
add_subdirectory(libs/dlib dlib_build)
add_library(face_align SHARED src/mylib.cpp)
# This makes the target position independent, allowing to link an static library to a dynamic one.
set_target_properties(face_align PROPERTIES POSITION_INDEPENDENT_CODE ON) # This made it work
# Link Dlib
target_link_libraries(face_align dlib::dlib)
修改强>
另外,在Linux中(我还没有在其他S.O.s中测试过这个)CMake做了一些魔术,在共享模式下安装dlib库之后:
$ cd $DLIB
$ mkdir build && cd build
$ cmake ../dlib
$ make && make install
可以使用target_link_libraries
简单地添加库,如下所示:
cmake_minimum_required(VERSION 2.8.12)
project(face_align)
set(CMAKE_CXX_STANDARD 11)
add_library(face_align SHARED src/mylib.cpp)
# Link Dlib
target_link_libraries(face_align dlib)
然后,在您的代码中,按如下方式包含标题:
#include <dlib/your_dlib_header_here.h>