编译器特性--实现自定义预编译宏开关
学习c++也有一段时间,但是对make的应运也是近期开始比较多的,在最近的学习中发现了一
个make支持编译器的特性可以运用宏配合#ifdef,#ifndef,#else,#endif等来制定预编译开关,从而通过
这些宏来选择要编译的代码段。下面是我自己试验的一个小例子。
hello.cpp文件内容下:
#include<iostream>
using namespace std;
int main(){
#ifdef TEST
cout<<"hello Test"<<endl;
#endif
cout<<"hello winxp"<<endl;
return 0;
}
makefile文件内容如下:
编译器
COMPILE=cl
#连接器
LINKER=link
#在制定编译器的选项,/c表明cl命令只编译不连接,/zi表明产生完整的调试信息,/Od表明
关闭编译优化 /D TEST定义了TEST宏 所以在编译过程中会将hello.cpp文件中#ifdef
....#endif之间的代码 ,若去掉则不会编译,可由运行结果验证。
CPP_FLAGS=/c /Zi /Od /DTEST
all:hello.exe
hello.obj:hello.cpp
cl $(CPP_FLAGS) hello.cpp
hello.exe:hello.obj
link hello.obj
clean:
del hello.exe
del hello.obj
del vc60.pdb
linux下的gcc,aCC,CC,g++等编译也支持这个特性。
浙公网安备 33010602011771号