GCC编译器使用学习笔记(一)

xiaoxiao2025-02-11  10

GCC编译器使用学习笔记 gcc: GNU C Compiler/GNU Compiler Collection gcc -v gcc --version:查看版本 vi hello.c #include<stdio.h>int main(void){ printf("Hello,world!\n"); return 0;} gcc -Wall hello.c -o hello (-Wall:打开gcc的所有警告) ./hello gcc -Wall hello.c ./a.out 测试-wall: #include<stdio.h>int main(void){ printf("2 + 2 = %d\n",4); //%f:会显示警告 return 0;} gcc -Wall hello.c hello --多文件编译: vi hello.h void hello(const char* string);//不能没有; vi hello.c #include<stdio.h>#include"hello.h"void hello(const char* string){ printf(string);} vi main.c #include<stdio.h> //系统头文件(/usr/include,/usr/local/include),不查找当前目录#include"hello.h" //先在当前目录下找int main(void){ hello("Hello,world!\n"); return 0;} gcc -Wall hello.c main.c -o hello2 ./hello2 --调错参数 -v gcc -v -Wall hello.c --只编译产生目标文件hello.o: -c gcc -Wall -c hello.c gcc -Wall -c main.c --目标文件产生可执行文件 gcc hello.o main.o -o hello ./hello --调用系统库 vi calc.c #include<math.h>#include<stdio.h>int main(void){ double x = sqrt(2.0); printf("the square root of 2.0 is %f.\n",x); return 0;} --调用第三方库 gcc -Wall main.c /usr/lib/libm.a -o calc --创建自己的库 ar ar cr libNAME.a file1.o file2.o ... filen.o vi mylib.h int func1(int x,int y);void func2(int x); vi func1.c #include "mylib.h"int func1(int x,int y){ return x+y;} vi func2.c #include <stdio.h>#include "mylib.h"void func2(int x){ printf("The result is %d.\n",x);} vi main.c #include <stdio.h>#include "mylib.h"int main(void){ int i; i = func1(1,2); func2(i); return 0;} 编译 gcc -Wall -c func1.c gcc -Wall -c func2.c 生产库文件 ar cr libhello.a func1.o func2.o gcc -Wall main.c libhello.a -o hello gcc -Wall -c main.c gcc -Wall func1.o func2.o main.o -o hello gcc -Wall libhello.a main.c -o hello //编译不成功 cp libhello.a /usr/lib //不推荐使用 gcc -Wall main.c -lhello -o hello //只在系统库中查找 gcc -Wall main.c -L. -lhello -o hello //相对路径,推荐使用 --定义环境变量 export LIBRARY_PATH=/home/mcfeng:$LIBRARY_PATH env | grep LIB gcc -Wall main.c -lhello -o hello --源程序结构 include lib main.c gcc -Wall -Iinclude -c main.c gcc -Wall main.o -Llib -lhello -o hello
转载请注明原文地址: https://www.6miu.com/read-5024499.html

最新回复(0)