以下是我的情景。我有一个顶级 CMakeList.txt 和另外两个内部 CMakeList.txt 。在顶级cmake中,我有3个自定义目标copy
,build
,copyandbuild
。由于name指定make copy
将源目录(即dir1,dir2)复制到$ {CMAKE_SOURCE_DIR}。 make build
创建库和可执行文件。 make copyandbuild
(复制+构建)。
从构建目录运行cmake ..
成功完成。
如果我运行make copyandbuild
它正在复制到$ {CMAKE_SOURCE_DIR},但在构建时它显示错误
No rule to make target `dir1/libmylib.so', needed by `CMaketargetdbuild'. Stop
如果我按以下顺序执行命令,它会工作。
我的要求是不应该运行cmake并再次进行构建,因为copyandbuild会做同样的工作。
顶级CMakeLists.txt:
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 2.6)
set(RE_BUILD make rebuild_cache)
set(OUTPUT_DIR ${CMAKE_SOURCE_DIR}/../)
if(EXISTS ${CMAKE_SOURCE_DIR}/dir1)
message(WARNING "Found dir1 dir")
add_subdirectory(dir1 EXCLUDE_FROM_ALL)
else()
message(WARNING "Couldn't find dir1 directory ")
endif()
if(EXISTS ${CMAKE_SOURCE_DIR}/dir2)
add_subdirectory(dir2 EXCLUDE_FROM_ALL)
else()
message(WARNING "Couldn't find dir2 directory")
endif()
set(MOVE_LIB_COMMAND mv src/myapp . && mv dir1/mylib .)
set(COPY_COMMAND cp -r ../sourceCode1 ../dir1 && cp -r ../sourceCode2 ../dir2)
set(CLEAN_DIR1_COMMAND cmake -E remove_directory ${CMAKE_SOURCE_DIR}/dir1)
set(CLEAN_DIR2_COMMAND cmake -E remove_directory ${CMAKE_SOURCE_DIR}/dir2)
set(SET_SLEEP sync)
#Copy command
add_custom_target(
copy ${COPY_COMMAND}
COMMAND ${RE_BUILD}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
)
#Compilation
add_custom_target(
build
COMMAND ${MOVE_LIB_COMMAND}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS mylib myapp
)
#copy and compile
add_custom_target(
copyandbuild
COMMAND ${MOVE_LIB_COMMAND}
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
DEPENDS copy mylib myapp
)
add_custom_command(TARGET copy POST_BUILD
COMMAND ${SET_SLEEP}
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
dir1 CMake是:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
include_directories(
${MY_APP_INCLUDE_DIRS}
)
link_directories(
${MY_APP_LIBDIR}
)
add_library(mylib
SHARED
com/*.cpp
)
target_link_libraries(mylib myapp_lib)
dir2 CMake是:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
include_directories(
${MY_APP_INCLUDE_DIRS}
)
link_directories(
${MY_APP_LIBDIR}
)
答案 0 :(得分:2)
您正在以阻止其正常运行的方式使用CMake。通过在许多地方显式调用shell命令,当您可以使用CMake内置功能时,您正在抢夺可用于构建程序的任何上下文的CMake。此外,在CMake中使用*.cpp
之类的通配符被认为是不好的做法。并且您有许多重复的语句 - 您不需要cmake_minimum_required()
或设置除顶层以外的编译器标志。
简而言之,顶级CMakeLists.txt
应该更像这样:
cmake_minimum_required(VERSION 2.6)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
add_subdirectory(dir1 EXCLUDE_FROM_ALL)
您不需要复制源文件 - 只需从它们所在的位置构建它们,例如您的dir1/CMakeLists.txt
可能是:
add_library(mylib
SHARED
sourceCode1/mylib.cpp
)
保持简单。让它工作。如果您需要添加缺失的功能,请提出问题。