版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433650
当我们制作用于二次开发的SDK的时候,用cmake的install命令制作一个安装脚本提供简单的SDK安装功能,在make结束后自动把文件分类放在不同的目录下,提供给用户,比只是把.h,.lib,.dll文件一股脑的复制给用户,让用户自己去配置要好得多。
下面的cmake脚本片段,展示了用cmake install命令实现的简单安装功能:
代码语言:javascript复制########安装脚本##########
# 将生成的target安装到指定目录下,这里 detect_cl,img_tool是动态库,test_detect是可执行程序,
# cmake会自动根据target的类型将可执行程序(如.exe)和动态库(如.dll)作为RUNTIME类型复制到bin下,
# 将动态库的导入库(.lib .a)作为ARCHIVE类型复制lib/static目录下
install(TARGETS detect_cl img_tool test_detect
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
# 复制源码中的指定的头文件到 include下
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/detect_cl_types.h DESTINATION include)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/detect_cl.h DESTINATION include)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/img_tool.h DESTINATION include)
# 复制sample代码到 sample下
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/test_detect.cpp DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/../codemgr/cmimpl/utility.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/../codemgr/cmimpl/raii.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/include/assert_macros.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/include/cmdline.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/include/dirent.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/include/file_utilits.h DESTINATION sample)
install(FILES ${PROJECT_SOURCE_DIR}/include/time_utilits.h DESTINATION sample)
然后执行 make install (gcc编译),在完成所有代码编译后,就自动把你指定的的文件复制到安装目录下。
如果你要指定不同的安装目录,在生成 Makefile的时候通过定义CMAKE_INSTALL_PREFIX变量来实现,如下:
cmake -G “Eclipse CDT4 - MinGW Makefiles” -DCMAKE_INSTALL_PREFIX=d:/detect_clundefined
cmake install命令还有更多复杂的应用方式,参见 https://cmake.org/cmake/help/v3.1/command/install.html