我有这个 CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
include(${CMAKE_CURRENT_SOURCE_DIR}/src/Something.cmake)
add_executable(execute main.cpp)
add_dependencies(somethingInterface Something)
add_dependencies(execute somethingInterface)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_options(execute
PRIVATE
-std=c++11
-g
)
add_library(library SHARED IMPORTED)
set_target_properties(library PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/library.so)
target_link_libraries(execute
PRIVATE
library
)
共享导入的库将在Something.cmake中创建,但必须首先构建。 这是 Something.cmake
中的add_custom_command(TARGET POST_BUILD ...)我在这里使用CMake版本没有问题,但是当我使用忍者时,会出现 错误 。
ninja: error: 'library.so', needed by 'execute', missing and no known rule to make it
或者您对如何执行此操作有任何建议?
我认为忍者有一个要求,即“ library.so”必须首先存在,但要保证两者的区别。它会在运行时检查该库是否存在。
答案 0 :(得分:3)
Make 和 Ninja 处理导入目标的方式确实存在分歧。对 Make 有用的东西,有时可能不适用于 Ninja。
特别是,以下代码行适用于 Make,但不适用于 Ninja:
ExternalProject_Add(extProject
GIT_REPOSITORY <GIT_URL>
CMAKE_CACHE_ARGS "-
DCMAKE_INSTALL_PREFIX:STRING=${CMAKE_INSTALL_PREFIX}"
)
add_library(extLib SHARED IMPORTED)
add_dependencies(extLib extProject)
set_target_properties(extLib
PROPERTIES IMPORTED_LOCATION ${CMAKE_INSTALL_PREFIX}/lib/libext.so
)
target_link_libraries(project extLib)
CMake 配置步骤可以正常工作,但在构建时 Ninja 会抱怨:
ninja: error: '/path/to/libext.so', needed by 'project', missing and no known rule to make it
但这对 Make 来说很好。
您需要将库指定为 ExternalProject_Add
注释的副产品,因为 ExternalProject 在构建时运行。
以下对我来说很好用:
ExternalProject_Add(extProject
GIT_REPOSITORY <GIT_URL>
CMAKE_CACHE_ARGS "-
DCMAKE_INSTALL_PREFIX:STRING=${CMAKE_INSTALL_PREFIX}"
BUILD_BYPRODUCTS ${CMAKE_INSTALL_PREFIX}/lib/libext.so
)
add_library(extLib SHARED IMPORTED)
add_dependencies(extLib extProject)
set_target_properties(extLib
PROPERTIES IMPORTED_LOCATION ${CMAKE_INSTALL_PREFIX}/lib/libext.so
)
target_link_libraries(project extLib)