install(TARGETS mylib mylib_shared EXPORT MyLibTargets ARCHIVE DESTINATION lib LIBRARY DESTINATION lib PUBLIC_HEADER DESTINATION include RUNTIME DESTINATION bin INCLUDES DESTINATION include)中PUBLIC_HEADER所代表的路径如何确定,举个例子
时间: 2023-08-13 17:03:12 浏览: 77
`PUBLIC_HEADER`选项所代表的路径是相对于安装目录的路径。在`install()`命令中,通过`PUBLIC_HEADER DESTINATION`选项来指定头文件的安装路径。例如,`PUBLIC_HEADER DESTINATION include`表示将头文件安装到`<prefix>/include`目录下,其中`<prefix>`是通过`CMAKE_INSTALL_PREFIX`选项设置的安装前缀。
举个例子,如果你在`CMakeLists.txt`文件中设置了以下内容:
```
install(TARGETS mylib
EXPORT MyLibTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include
INCLUDES DESTINATION include)
install(EXPORT MyLibTargets
FILE MyLibTargets.cmake
NAMESPACE MyLib::
DESTINATION lib/cmake/MyLib)
```
那么,当你执行`make install`命令时,CMake会将`mylib.h`头文件复制到`<prefix>/include`目录下,其中`<prefix>`是通过`CMAKE_INSTALL_PREFIX`选项设置的安装前缀。如果`CMAKE_INSTALL_PREFIX`选项设置为`/usr/local`,则`mylib.h`将会被安装到`/usr/local/include`目录下。
如果你想将`mylib.h`安装到`<prefix>/include/mylib`目录下,则可以将`PUBLIC_HEADER DESTINATION`选项设置为`include/mylib`。例如:
```
install(TARGETS mylib
EXPORT MyLibTargets
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include/mylib
INCLUDES DESTINATION include)
install(EXPORT MyLibTargets
FILE MyLibTargets.cmake
NAMESPACE MyLib::
DESTINATION lib/cmake/MyLib)
```
注意,`PUBLIC_HEADER`选项只对静态库和动态库有效。如果你的库是一个可执行文件,则不需要使用`PUBLIC_HEADER`选项。
阅读全文