我是一只小小鸟
全部博文(184)
分类: C/C++
2014-03-24 16:29:28
#include int main(void){ printf ("Hello world, Linux programming!/n"); return 0; } |
# gcc hello.c -o hello # ./hello Hello world, Linux programming! |
# gcc -E hello.c -o hello.i |
# gcc -c hello.i -o hello.o |
# gcc hello.o -o hello |
# gcc foo1.c foo2.c -o foo |
# gcc -c foo1.c -o foo1.o # gcc -c foo2.c -o foo2.o # gcc foo1.o foo2.o -o foo |
#include void main(void){ long long int var = 1; printf("It is not standard C code!/n"); } |
# gcc -pedantic illcode.c -o illcode illcode.c: In function `main': illcode.c: 9: ISO C89 does not support `long long'illcode.c: 8: return type of `main' is not `int' |
# gcc -Wall illcode.c -o illcode illcode.c: 8: warning: return type of `main' is not `int'illcode.c: In function `main':illcode.c: 9: warning: unused variable `var' |
# gcc -Wall -Werror illcode.c -o illcodecc 1: warnings being treated as errorsillcode.c: 8: warning: return type of `main' is not `int'illcode.c: In function `main':illcode.c: 9: warning: unused variable `var' |
# gcc foo.c -I /home/xiaowp/include -o foo |
# gcc foo.c -L /home/xiaowp/lib -lfoo -o foo |
# gcc foo.c -L /home/xiaowp/lib -static -lfoo -o foo |
#include int main(void){ double counter; double result; double temp; for (counter = 0; counter < 2000.0 * 2000.0 * 2000.0 / 20.0 + 2020; counter += (5 - 1) / 4) { temp = counter / 1979; result = counter; } printf("Result is %lf/n", result); return 0; } |
# gcc -Wall optimize.c -o optimize |
# time ./optimize Result is 400002019.000000 real 0m14.942s user 0m14.940s sys 0m0.000 s |
# gcc -Wall -O optimize.c -o optimize |
# time ./optimize Result is 400002019.000000 real 0m3.256s user 0m3.240s sys 0m0.000s |
# gcc optimize.c -o optimize # ls optimize -l -rwxrwxr-x 1 xiaowp xiaowp 11649 Nov 20 08:53 optimize (未加调试选项) # gcc -g optimize.c -o optimize # ls optimize -l -rwxrwxr-x 1 xiaowp xiaowp 15889 Nov 20 08:54 optimize (加入调试选项) |
#include int main(void){ int input =0; printf("Input an integer:"); scanf("%d", input); printf("The integer you input is %d/n", input); return 0; } |
# gcc -g crash.c -o crash # ./crash Input an integer:10 Segmentation fault |
# gdb crashGNU gdb Red Hat Linux (5.3post-0.20021129.18rh)…… (gdb) |
(gdb) run Starting program: /home/xiaowp/thesis/gcc/code/crash Input an integer:10 Program received signal SIGSEGV, Segmentation fault.0x4008576b in _IO_vfscanf_internal () from /lib/libc.so.6 |
(gdb) backtrace #0 0x4008576b in _IO_vfscanf_internal () from /lib/libc.so.6 #1 0xbffff0c0 in ?? () #2 0x4008e0ba in scanf () from /lib/libc.so.6 #3 0x08048393 in main () at crash.c:11 #4 0x40042917 in __libc_start_main () from /lib/libc.so.6 |
(gdb) frame 3 #3 0x08048393 in main () at crash.c:1111 scanf("%d", input); |
scanf("%d", input);改为scanf("%d", &input); |
(gdb) quit |
# gcc -save-temps foo.c -o foo # ls foo*foo foo.c foo.i foo.s |
# gcc -pipe foo.c -o foo |