记住cmake代码cmake_minimum_required(VERSION 3.16) # 声明该项目的名称和版本号 project(MyLib VERSION 1.0) # 添加库代码文件到该库 add_library(mylib STATIC src/header.cpp) add_library(mylib_shared SHARED src/header.cpp) include_directories(include) # 指定install路径,以便于其他项目找到该库 install(TARGETS mylib mylib_shared EXPORT MyLibTargets ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin) install(FILES include/header.h DESTINATION include) # 生成MyLibConfig.cmake文件 include(CMakePackageConfigHelpers) write_basic_package_version_file( "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake" VERSION ${MyLib_VERSION} COMPATIBILITY AnyNewerVersion ) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/MyLibConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake" INSTALL_DESTINATION cmake ) # install( # EXPORT MyLibTargets # NAMESPACE MyLib:: # DESTINATION cmake # ) export(EXPORT MyLibTargets NAMESPACE MyLib:: FILE MyLibTargets.cmake) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/MyLibTargets.cmake DESTINATION cmake) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake" DESTINATION cmake ),并在/home/czl/cmake_lib/build目录下执行命令cmake .. -DCMAKE_INSTALL_PREFIX=/home/czl/pkg_test,并告诉我生成的MyLibTargets.cmake文件中,静态库的引用路径是什么
时间: 2024-02-18 12:01:51 浏览: 66
cmake_min_version:确定项目的最低要求CMake版本
生成的MyLibTargets.cmake文件中,静态库的引用路径是:
```
target_link_libraries(MyLib::mylib INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../lib/libmylib.a>
$<INSTALL_INTERFACE:lib/libmylib.a>
)
```
其中,`$<BUILD_INTERFACE>`指的是在构建项目时引用的路径,`$<INSTALL_INTERFACE>`指的是在安装项目后引用的路径。在这个例子中,静态库`libmylib.a`在构建项目时的路径为`${CMAKE_CURRENT_LIST_DIR}/../lib/libmylib.a`,在安装项目后的路径为`lib/libmylib.a`。
阅读全文