通过增加编译宏实现条件编译
ADD_DEFINITIONS(-D宏名称 -D宏名称......)
camke文件如下:
cmake_minimum_required(VERSION 2.8)
PROJECT (hello)
SET(SRC_LIST main.c)
MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR})
add_definitions(-Dhha -Dbbb)
ADD_EXECUTABLE(hello ${SRC_LIST})
源代码如下:
#include<stdio.h>
int main()
{
#ifdef hha
printf("include hhahah.\n");
#endif
#ifdef bbb
printf("include bbbbbb.\n");
#endif
printf("hello,word.\n");
return 0;
}
输出结果: