2018年1月23日 下午7:24
目的:
这篇文章的目的:是为了说明使用gcc的意义
hello.c(包含三处错误,分别对应图1中1~3)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #include <stdio.h>
#define MAX 20 #define MIN 10
#define _DEBUG #define SetBit(x) (1<<x)
int mian(int argc, char* argv[])#3.这里mian不对 { print("Hello world \n);#1.这里少引号 print("MAX = %d, MIN = %d, MAX + MIN = %d\n",MAX, MIN, MAX+MIN);#2.print应该是printf
#ifdef _DEBUG print("SetBit(5) = %d, SetBit(6) = %d\n",SetBit(5),SetBit(6)); print("SetBit( SetBit(2) ) = %d\n",SetBit( SetBit(2) )); #endif return 0; }
|
在ubuntu下hello.c的整体编译过程:

::图1::
总结:
- 这里的错误可以看出分别产生自预处理,编译,链接时,这就体现了gcc比我们一体化的IDE的优势,IDE总是一股脑的全部给你报错,不分阶段。
- 如我做一下工作,gcc会帮你解决
- 如何看到宏,如何工作?从预处理中.i文件最后就可以看出(E)
- 如何看到我们汇编代码?从编译后的.s文件就可以看出(S)
- 库文件函数,何时与自己的代码融合?从连接中就可看出
- 为此,我们要清楚的知道.c .i .s .o 文件内存的是啥?关于这个问题,可以看附录中的具体代码。
附录:
文件源码:
hello.chello.ihello.s
hello.o
hello
老师的笔记:
gcc常用编译选项.txt
ubuntu操作代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
| book@www.100ask.org:/$ cd /work book@www.100ask.org:/work$ ls 100ask hardware lost+found tools book@www.100ask.org:/work$ mkdir gcc_option book@www.100ask.org:/work$ ls 100ask gcc_option hardware lost+found tools book@www.100ask.org:/work$ ls 100ask gcc_option hardware lost+found tools book@www.100ask.org:/work$ cd gcc_option/ book@www.100ask.org:/work/gcc_option$ ls hello.c book@www.100ask.org:/work/gcc_option$ gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.4' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4) book@www.100ask.org:/work/gcc_option$ gcc -V gcc: error: unrecognized command line option ‘-V’ gcc: fatal error: no input files compilation terminated. book@www.100ask.org:/work/gcc_option$ gcc -E -o hello.i hello.c hello.c:11:8: warning: missing terminating " character print("Hello world \n); ^ book@www.100ask.org:/work/gcc_option$ gcc -E -o hello.i hello.c book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i book@www.100ask.org:/work/gcc_option$ gcc -S -o hello.s hello.i hello.c: In function ‘mian’: hello.c:11:2: warning: implicit declaration of function ‘print’ [-Wimplicit-function-declaration] print("Hello world \n"); ^ book@www.100ask.org:/work/gcc_option$ gcc -E -o hello.i hello.c book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i hello.s book@www.100ask.org:/work/gcc_option$ rm * -rf book@www.100ask.org:/work/gcc_option$ ls book@www.100ask.org:/work/gcc_option$ gcc -E -o hello.i hello.c book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i book@www.100ask.org:/work/gcc_option$ gcc -S -o hello.s hello.i book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i hello.s book@www.100ask.org:/work/gcc_option$ gcc -c -o hello.o hello.s book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i hello.o hello.s book@www.100ask.org:/work/gcc_option$ gcc -o hello hello.o /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o: In function `_start': (.text+0x20): undefined reference to `main' collect2: error: ld returned 1 exit status book@www.100ask.org:/work/gcc_option$ ls hello.c hello.i hello.o hello.s book@www.100ask.org:/work/gcc_option$ ^C book@www.100ask.org:/work/gcc_option$ rm * -rf book@www.100ask.org:/work/gcc_option$ ls book@www.100ask.org:/work/gcc_option$ gcc -E -o hello.i hello.c book@www.100ask.org:/work/gcc_option$ gcc -S -o hello.s hello.i book@www.100ask.org:/work/gcc_option$ gcc -c -o hello.o hello.s book@www.100ask.org:/work/gcc_option$ gcc -o hello hello.o book@www.100ask.org:/work/gcc_option$ ls hello hello.c hello.i hello.o hello.s book@www.100ask.org:/work/gcc_option$ ./hello Hello world MAX = 20, MIN = 10, MAX + MIN = 30 SetBit(5) = 32, SetBit(6) = 64 SetBit( SetBit(2) ) = 16
|