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

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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值