如何在构建另一个之前告诉CMake构建并运行可执行文件?所以我有2个可执行文件"a"和"b",其中"a"需要运行才能生成"b"的头文件.因此"a"将2个文件夹作为参数,在其中从xml文件生成头文件,从输入目录到输出目录.
有没有办法告诉CMake这样做,以及知道修改xml文件的时间或修改项目"a"以重新生成文件?
use*_*253 13
如果test1根据test1.c需要在事先执行构建之前test2构建test2.c,那么解决方案应如下所示:
- test1.c -
#include <stdio.h>
int main(void) {
printf("Hello world from test1\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
- test2.c -
#include <stdio.h>
int main(void) {
printf("Hello world from test2\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
- 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)
Run Code Online (Sandbox Code Playgroud)
它生成以下输出:
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
Run Code Online (Sandbox Code Playgroud)
add_custom_command如果您的任务需要,您可能还需要使用和其他相关的CMake指令.