以下是我的方案。我有一個頂級CMakeList.txt和另外兩個內部CMakeList.txt。在頂級cmake我有3個自定義目標是copy
,build
,copyandbuild
。如名稱指定make copy
將源目錄(即dir1,dir2)複製到$ {CMAKE_SOURCE_DIR}。 make build
創建庫和可執行文件。 make copyandbuild
(copy + build)。如何重新加載cmake目標
從build目錄運行cmake ..
成功完成。
如果我運行make copyandbuild
它複製到$ {CMAKE_SOURCE_DIR},但在構建時它顯示錯誤
No rule to make target `dir1/libmylib.so', needed by `CMaketargetdbuild'. Stop
- MyProject的
- DIR1
- CMakeLists。 txt
- DIR2
- 的CMakeLists.txt
- 的CMakeLists.txt
- DIR1
它正在工作,如果我在下面的順序執行命令。
- cmake的..
- 化妝copyandbuild
- cmake的..
- 使建立
我的要求是它應該再次出來工作運行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}
)
你能分享一下CmakeLists.txt的內容嗎? – Hayt
請清理你的代碼片段:使變量保持一致(例如,設置變量RE_BUILD_COMMAND,但使用RE_BUILD),刪除片段中未使用的變量的定義。就我所瞭解的代碼而言,你期待[add_custom_target]的DEPENDS選項(https://cmake.org/cmake/help/v3.6/command/add_custom_target.html)添加來自其他*目標*的依賴關係。但它實際上增加了**文件**的依賴關係。要添加目標之間的依賴關係,請使用命令[add_dependencies](https://cmake.org/cmake/help/v3.6/command/add_dependencies.html#command:add_dependencies)。 – Tsyvarev