make[2]: *** 没有规则可制作目标“/home/czl/cmake_test/lib/mylib”,由“main” 需求。 停止是什么意思
时间: 2023-12-27 17:05:16 浏览: 165
这个错误信息是在使用make命令时出现的。它的意思是,make在尝试构建一个名为“/home/czl/cmake_test/lib/mylib”的目标时失败了,因为没有找到构建规则。这个目标是由名为“main”的目标所需的。因此,make无法继续构建并停止了。通常,这种错误是由Makefile中的错误或缺少所致。您需要检查Makefile中的规则和目标,以确保它们正确配置。
相关问题
记住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文件中,静态库的引用路径是什么
生成的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`。
cmake_minimum_required(VERSION 3.16) # 声明该项目的名称和版本号 project(MyLib VERSION 1.0) # 添加库代码文件到该库 add_library(mylib STATIC src/header.cpp) target_include_directories(mylib INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) add_library(mylib_shared SHARED src/header.cpp) target_include_directories(mylib_shared INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include> ) # 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/src/header.cpp:1:10: fatal error: header.h: 没有那个文件或目录
在你的 `CMakeLists.txt` 文件中,你已经将 `include/header.h` 安装到了 `include` 目录中,但是在 `add_library` 命令中没有将该目录添加到编译器的 include 路径中,所以编译器无法找到该头文件。
你可以将 `target_include_directories` 命令添加到 `add_library` 命令中,这样就可以将 `include` 目录添加到编译器的 include 路径中了。修改后的代码如下所示:
```cmake
cmake_minimum_required(VERSION 3.16)
project(MyLib VERSION 1.0)
# 添加库代码文件到该库
add_library(mylib STATIC src/header.cpp)
target_include_directories(mylib
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
add_library(mylib_shared SHARED src/header.cpp)
target_include_directories(mylib_shared
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE: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
)
install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/MyLibConfig.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/MyLibConfigVersion.cmake"
DESTINATION cmake
)
```
注意,在这里,我们使用了 `PUBLIC` 修饰符,将 include 路径添加到了 `mylib` 和 `mylib_shared` 目标的 include 路径中,这样在编译 `mylib` 和 `mylib_shared` 时都可以找到 `include/header.h` 头文件。
阅读全文