我想在项目的make install安装期间将一组标题(与目录中的所有标题不同)安装到include目录。
这是我的文件结构:
MyProject
|-build
|-lib
|-include
|-src
|-A
| |-a1.cpp/.h
| |-a2.cpp/.h
| |-a3.cpp/.h
|
|-B
| |-b1.cpp/.h
| |-b1.cpp/.h
|
|-CMakeLists.txt
在MyProject/src/CMakeLists.txt
(这是主要的CMakeLists文件)中,我设置了我的项目并将所有内容编译到库中。
在安装步骤(即make install)期间,我想将构建libmyproject.a
移至MyProject/lib
和部分标头,例如A/a1.h
和B/b1.h
到MyProject/include
文件夹。
但是,我想维护这些标题的相对路径!
即,我想实现这个目标:
MyProject
|-build
|-lib
| |-libmyproject.a
|
|-include
| |-A
| | |-a1.h
| |
| |-B
| |-b1.h
|
|-src
|-...
使用CMake轻松安装库:
set(CMAKE_INSTALL_PREFIX“path_to_my_project”)
安装(TARGETS myproject
ARCHIVE DESTINATION lib
)
我奋斗的地方是标题。我有一个简单的API标头列表:
组(my_api_headers A / a1.h B / b1.h )
但是我不能简单地使用'install(FILES ...)'CMake命令来消除相对路径。到目前为止,我发现的所有其他建议都与'PATTERN'的'install(DIRECTORY ...)'形式有关,但我不认为我可以使用那个 会复制所有标题。 (比较,例如,How can I install a hierarchy of files using cmake?)
我尝试了一个简单的功能
############################################################
#
# FUNCTION: install API header files
#
function(install_api_headers
API_HDRS
DST_DIR
)
foreach( _header ${${API_HDRS}} )
file(INSTALL ${_header}
DESTINATION ${DST_DIR}
)
endforeach( _header )
endfunction(install_api_headers)
#
############################################################
install_api_headers(my_api_headers "MyProject/include")
但显然(好吧,我记得当我运行它时:...)在CMake运行时执行而不是'make install'... 尝试使用'install(CODE ...)'包装器进行简单修复也不起作用...... :(
install(CODE“install_api_headers(workmodel_API_HDRS {CMAKE_INSTALL_PREFIX} / $ {} include_DIR_NAME / WorkModels / $ {} workmodel_NAME)” )
所以,底线:非常感谢任何有关如何完成此工作的提示,想法,链接。
谢谢,
克劳斯
答案 0 :(得分:2)
semipol.de/archives/251似乎解决了这个问题:
从链接:
SET(HS folder/test.h folder/other/test2.h)
A simple call to INSTALL doesn’t preserve the folder structure:
INSTALL(FILES ${HS} DESTINATION include)
This results in all files being directly under $prefix/include.
To preserve the structure you can use this simple macro:
MACRO(INSTALL_HEADERS_WITH_DIRECTORY HEADER_LIST)
FOREACH(HEADER ${${HEADER_LIST}})
STRING(REGEX MATCH "(.*)[/\]" DIR ${HEADER})
INSTALL(FILES ${HEADER} DESTINATION include/${DIR})
ENDFOREACH(HEADER)
ENDMACRO(INSTALL_HEADERS_WITH_DIRECTORY)
INSTALL_HEADERS_WITH_DIRECTORY(HS)
我做了一个例子,我认为解决了问题(我的方式:)):
https://dl.dropboxusercontent.com/u/68798379/cmake-install-example.tar.bz2
$ ./gen-linux.sh
-- The C compiler identification is GNU 4.8.2
..
$ cd linux/
$ make
Scanning dependencies of target LIBB
[ 20%] Building CXX object src/B/CMakeFiles/LIBB.dir/testB.cpp.o
[ 40%] Building CXX object src/B/CMakeFiles/LIBB.dir/internalB.cpp.o
Linking CXX static library libLIBB.a
[ 40%] Built target LIBB
Scanning dependencies of target LIBA
[ 60%] Building CXX object src/A/CMakeFiles/LIBA.dir/testA.cpp.o
[ 80%] Building CXX object src/A/CMakeFiles/LIBA.dir/internalA.cpp.o
Linking CXX static library libLIBA.a
[ 80%] Built target LIBA
Scanning dependencies of target TESTLIB
[100%] Building C object CMakeFiles/TESTLIB.dir/src/dummy.c.o
Linking C static library libTESTLIB.a
[100%] Built target TESTLIB
$ make install
[ 40%] Built target LIBB
[ 80%] Built target LIBA
[100%] Built target TESTLIB
Install the project...
-- Install configuration: ""
-- Installing: cmake-install-example/build/linux/../../lib/libTESTLIB.a
-- Installing: cmake-install-example/build/linux/../../include/testA.hpp
-- Installing: cmake-install-example/build/linux/../../include/testB.hpp
亮点:
请注意build / gen-linux.sh脚本中CMAKE_INSTALL_PREFIX的设置。