我有一个库,该库已构建并链接到我的项目中。我想提供的工具可以在系统范围内(或设置$ {CMAKE_INSTALL_PREFIX}的任何位置)安装该库。否则,默认情况下,项目的最终构建产品将静态链接到库,并且前者将被安装,但库二进制文件仍保留在构建目录中。
换句话说:
$ make $ make install
将生成和安装程序,但仅类似
$ make install.foo
将库安装到$ {CMAKE_INSTALL_PREFIX},并在需要时首先构建它。
到目前为止,我有类似的内容(从实际脚本中简化,因此可能会有错误):
INCLUDE_DIRECTORIES( "${CMAKE_CURRENT_LIST_DIR}") SET (FOO_LIBRARY "foo") # Following builds library and makes it available to # be linked other targets within project by: # TARGET_LINK_LIBRARIES(${progname} ${FOO_LIBRARY}) ADD_LIBRARY(${FOO_LIBRARY} foo/foo.cpp # and other sources ... ) ########################################################### # Approach #1 # ----------- # Optionally allow users to install it by invoking: # # cmake .. -DINSTALL_FOO="yes" # # This works, but it means that users will have to run # cmake again to switch back and forth between the libary # installation and non-library installation. # OPTION(INSTALL_FOO "Install foo" OFF) IF (INSTALL_FOO) INSTALL(TARGETS ${FOO_LIBRARY} DESTINATION lib/foo) SET(FOO_HEADERS foo/foo.h) INSTALL(FILES ${FOO_HEADERS} DESTINATION include/foo) UNSET(INSTALL_FOO CACHE) ENDIF() ########################################################### ########################################################### # Approach #2 # ----------- # Optionally allow users to install it by invoking: # # make install.foo # # Unfortunately, this gets installed by "make install", # which I want to avoid SET(FOO_INSTALL "install.foo") ADD_CUSTOM_TARGET(${FOO_INSTALL} COMMAND ${CMAKE_COMMAND} -D COMPONENT=foo -P cmake_install.cmake) ADD_DEPENDENCIES(${FOO_INSTALL} ${FOO_LIBRARY}) INSTALL(TARGETS ${FOO_LIBRRARY} DESTINATION lib/foo COMPONENT foo) SET(FOO_HEADERS foo/foo.h) INSTALL(FILES ${FOO_HEADERS} DESTINATION include/foo COMPONENT foo) ###########################################################
可以看出,方法#1可以完成,但是安装该库所需的步骤是:
$ cmake .. -DINSTALL_FOO="yes" $ make && make install
然后,要返回到“正常”构建,用户必须记住要再次运行cmake,而无需使用“ -DINSTALL_FOO”选项,否则该库将安装在下一个“ make install”中。
第二种方法在我运行“ make install.foo”时有效,但是如果我运行“ make install”,它也将安装库。我想避免后者。
有人对如何实现这一目标有任何建议吗?
我有一个库,该库已构建并链接到我的项目中。我想提供的工具可以在系统范围内(或设置$ {CMAKE_INSTALL_PREFIX}的任何位置)安装该库。 ...