我如何告诉CMake在构建另一个之前构建并运行可执行文件?所以我有2个可执行文件" a"和" b",其中" a"需要运行才能为" b"生成头文件。所以" a"将2个文件夹作为参数,在其中从xml文件生成头文件,从输入目录到输出目录。
有没有办法告诉CMake这样做,以及知道修改xml文件的时间或项目" a"被修改为重新生成文件?
答案 0 :(得分:12)
如果从test1
构建test1.c
需要事先执行从test2
构建的test2.c
,那么解决方案应该如下所示:
- test1.c -
#include <stdio.h>
int main(void) {
printf("Hello world from test1\n");
return 0;
}
- test2.c -
#include <stdio.h>
int main(void) {
printf("Hello world from test2\n");
return 0;
}
- CMakeLists.txt -
cmake_minimum_required(VERSION 2.8.11)
project(Test)
set(test1_SOURCES test1.c)
set(test2_SOURCES test2.c)
add_executable(test1 ${test1_SOURCES})
add_executable(test2 ${test2_SOURCES})
add_custom_target(test2_run
COMMAND test2
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "run generated test2 in ${CMAKE_CURRENT_SOURCE_DIR}"
SOURCES ${test2_SOURCES}
)
add_dependencies(test1 test2_run)
它会生成以下输出:
alex@rhyme cmake/TestDep/build $ cmake ..
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/alex/tmp/cmake/TestDep/build
alex@rhyme cmake/TestDep/build $ make test1
Scanning dependencies of target test2
[ 25%] Building C object CMakeFiles/test2.dir/test2.c.o
Linking C executable test2
[ 25%] Built target test2
Scanning dependencies of target test2_run
[ 50%] run generated test2 in /home/alex/tmp/cmake/TestDep
Hello world from test2
[ 75%] Built target test2_run
Scanning dependencies of target test1
[100%] Building C object CMakeFiles/test1.dir/test1.c.o
Linking C executable test1
[100%] Built target test1
如果您的任务需要,您可能还需要使用add_custom_command
和其他相关的CMake指令。