Linux 下GCC的于处理器CPP使用实例
时间:2014-10-30 12:04 来源:linux.it.net.cn 作者:it
[root@localhost ansi]# cpp -dM /dev/null //查看GCC内部自定义的宏
第一:通过使用宏去掉注释
[root@localhost ansi]# vim dtest.c
#include <stdio.h>
int main(void)
{
#ifdef TEST
printf("Test mode\n");
#endif
printf("Tuning......\n");
return 0;
}
1、使用宏(拿掉#if 0 …………#end if的注释)
[root@localhost ansi]# gcc -Wall -O -DTEST dtest.c -o dt
[root@localhost ansi]# ./dt
Test mode
Tuning......2、不使用宏
[root@localhost ansi]# gcc -Wall -O dtest.c -o wanyan
[root@localhost ansi]# ./wanyan
Tuning......
第二:通过宏实现赋值
[root@localhost ansi]# vim num.c
#include <stdio.h>
int main(void)
{
printf("Value of NUM is %d\n",NUM);
return 0;
}
1、只写-D默认的值为1(正确)
[root@localhost ansi]# gcc -Wall -O -DNUM num.c -o num
[root@localhost ansi]# ./num
Value of NUM is 1
2、给D赋值(正确)
[root@localhost ansi]# gcc -Wall -O -DNUM=123 num.c -o wanyan
[root@localhost ansi]# ./wanyan
Value of NUM is 123
3、不写-D(错误)
[root@localhost ansi]# gcc -Wall -O num.c -o ethnicitybeta
num.c: In function 'main':
num.c:5: error: 'NUM' undeclared (first use in this function)
num.c:5: error: (Each undeclared identifier is reported only once
num.c:5: error: for each function it appears in.)
第三:编译的过程(模拟),过程.c(原始代码部分)-->.i(预处理的部分)-->.s(汇编语言部分)-->.o(包含机器码的部分,目标文件)-->可执行的文件
[root@localhost test]# vim bad.c
#include <stdio.h>
int main(void)
{
printf("2 plus 2 is %d\n",4);
return 0;
}
~
下边这个步骤就是为了保存预处理的结果文件
[root@localhost test]# gcc -Wall -O -c -save-temps bad.c //重点在-save-temps的使用
[root@localhost test]# ls
bad.c bad.i bad.o bad.s
第四:GCC –E来查看过程(并不是真正的编译)
[root@localhost test]# vim bad.c
#include <stdio.h>
int main(void)
{
printf("2 plus 2 is %d\n",4);
return 0;
}
[root@localhost test]# gcc -E bad.c
………..
…………
………..
extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__));
# 844 "/usr/include/stdio.h" 3 4
# 2 "bad.c" 2
int main(void)
{
printf("2 plus 2 is %d\n",4);
return 0;
}
实验结束
(责任编辑:IT)
[root@localhost ansi]# cpp -dM /dev/null //查看GCC内部自定义的宏 第一:通过使用宏去掉注释 [root@localhost ansi]# vim dtest.c
#include <stdio.h>
int main(void) { #ifdef TEST printf("Test mode\n"); #endif printf("Tuning......\n"); return 0; } 1、使用宏(拿掉#if 0 …………#end if的注释) [root@localhost ansi]# gcc -Wall -O -DTEST dtest.c -o dt [root@localhost ansi]# ./dt Test mode Tuning......2、不使用宏 [root@localhost ansi]# gcc -Wall -O dtest.c -o wanyan [root@localhost ansi]# ./wanyan Tuning...... 第二:通过宏实现赋值 [root@localhost ansi]# vim num.c
#include <stdio.h>
int main(void) { printf("Value of NUM is %d\n",NUM); return 0; } 1、只写-D默认的值为1(正确) [root@localhost ansi]# gcc -Wall -O -DNUM num.c -o num [root@localhost ansi]# ./num Value of NUM is 1 2、给D赋值(正确) [root@localhost ansi]# gcc -Wall -O -DNUM=123 num.c -o wanyan [root@localhost ansi]# ./wanyan Value of NUM is 123 3、不写-D(错误) [root@localhost ansi]# gcc -Wall -O num.c -o ethnicitybeta num.c: In function 'main': num.c:5: error: 'NUM' undeclared (first use in this function) num.c:5: error: (Each undeclared identifier is reported only once num.c:5: error: for each function it appears in.) 第三:编译的过程(模拟),过程.c(原始代码部分)-->.i(预处理的部分)-->.s(汇编语言部分)-->.o(包含机器码的部分,目标文件)-->可执行的文件 [root@localhost test]# vim bad.c
#include <stdio.h>
int main(void) { printf("2 plus 2 is %d\n",4); return 0; } ~ 下边这个步骤就是为了保存预处理的结果文件 [root@localhost test]# gcc -Wall -O -c -save-temps bad.c //重点在-save-temps的使用 [root@localhost test]# ls bad.c bad.i bad.o bad.s 第四:GCC –E来查看过程(并不是真正的编译) [root@localhost test]# vim bad.c
#include <stdio.h>
int main(void) { printf("2 plus 2 is %d\n",4); return 0; }
[root@localhost test]# gcc -E bad.c ……….. ………… ……….. extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__)); # 844 "/usr/include/stdio.h" 3 4
# 2 "bad.c" 2
int main(void) { printf("2 plus 2 is %d\n",4); return 0; } 实验结束 (责任编辑:IT) |