├── cmakelists.txt
└── main.c
源文件
main.c
#include
int main(int argc, char *argv[])
{
printf("compile-flags:cmake\r\n");
// only print if compile flag set
#ifdef ex2
printf("hello compile flag ex2!\r\n");
#endif
#ifdef ex3
printf("hello compile flag ex3!\r\n");
#endif
return 0;
}
cmakelists.txt
cmake_minimum_required(version 3.5)
# set a default c compile flag
#set (cmake_c_flags "${cmake_c_flags} -dex2" cache string "set c compiler flags" force)
add_compile_options(-dex2)
# set the project name
project (compile_flags)
# add an executable
add_executable(cmake_examples_compile_flags main.c)
target_compile_definitions(cmake_examples_compile_flags
private ex3
)
第二行中add_compile_options 给编译链增加编译参数。
例如:add_compile_options(-std=c 11)
#在编译选项中加入c 11支持
编译
$ mkdir build
$ cd build/
$ cmake ..
$ make verbose=1 (打印详细的编译信息)
./cmake_examples_compile_flags
compile-flags:cmake
hello compile flag ex2!
hello compile flag ex3!