1、#if 0 else
#if 1
A // 代码块A
#else
B // 代码块B
#endif
系统只编译if 1
部分代码而不编译B部分代码,如果发行代码和调试代码是不同的,可以通过配置if 1/if 0
来选择编译部分,这是一个很常见的做法。
学习链接:c语言中“#if 0 / #if 1 … #endif”的作用
2、编译宏应用
int x = 3;
int main()
{
#if AA
x = 4;
#else
x = 5;
#endif
cout<<x<<endl;
getchar();
}
编译:
g++ test1.cpp -D AA 输出:4
g++ test1.cpp -D BB 输出:5
在cmake文件中添加编译选项用:target_compile_definitions()