cmakelist.txt中代码为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) # set(PUBLIC_HEADER) # 指定install路径,以便于其他项目找到该库 install(TARGETS mylib mylib_shared EXPORT MyLibConfig ARCHIVE DESTINATION lib LIBRARY DESTINATION lib RUNTIME DESTINATION bin INCLUDES DESTINATION include) install(FILES include/header.h DESTINATION include) # 生成MyLibConfig.cmake文件 include(CMakePackageConfigHelpers) write_basic_packMyLibConfigage_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( FILES "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake" DESTINATION cmake ),同级目录下的MyLibConfig.cmake.in代码为# 指定该项目的名称和版本号 set(MyLib_VERSION @MyLib_VERSION@) set(MyLib_INCLUDE_DIRS "@CMAKE_INSTALL_PREFIX@/include") set(MyLib_LIBRARIES "@CMAKE_INSTALL_PREFIX@/lib/libmylib.a") set(MyLib_LIBRARIES_SHARED "@CMAKE_INSTALL_PREFIX@/lib/libmylib_shared.so") # 导入MyLib的目标 include("${CMAKE_CURRENT_LIST_DIR}/MyLibTargets.cmake"),同级目录下的MyLibTargets.cmake代码为# 导入mylib静态库 add_library(MyLib::mylib STATIC IMPORTED) set_target_properties(MyLib::mylib PROPERTIES IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/lib/libmylib.a" ) # 导入mylib_shared动态库 add_library(MyLib::mylib_shared SHARED IMPORTED) set_target_properties(MyLib::mylib_shared PROPERTIES IMPORTED_LOCATION "@CMAKE_INSTALL_PREFIX@/lib/libmylib_shared.so" ) # 导出MyLib的目标 install( EXPORT MyLibConfig NAMESPACE MyLib:: DESTINATION cmake ),使用以上代码执行make install命令安装相应的cmake包,但是为什么在其他cmake文件中使用find_package()时,无法找到对应的包文件
时间: 2024-02-12 08:06:25 浏览: 120
cmake-3.30.1-windows-x86_64.zip
5星 · 资源好评率100%
在使用find_package()时,需要在cmake中指定搜索的路径,例如:
```
set(CMAKE_PREFIX_PATH /path/to/installation/directory)
find_package(MyLib REQUIRED)
```
其中,`/path/to/installation/directory`是你安装MyLib库的路径。另外,还需要在MyLibConfig.cmake文件中添加以下内容:
```
include(CMakeFindDependencyMacro)
find_dependency(MyLib)
```
这样就可以使用find_package()找到你安装的MyLib库了。
阅读全文