第一个.c文件编译

.c代码
#include <stdio.h>
#define STRING "hello word\n"

int main(){
  printf(STRING);
}

主要步骤

1、预编译 pre-processing

  • gcc -E test.c -o test.i

2、编译 compilation

  • gcc -S test.i -o test.s
  • gcc -Wall -S test.i -o test.s 可以打印所有的报错信息

3、汇编 asserbly

  • as test.s -o test.o .o文件是0、1机器语言
  • 可以直接一步到.o文件 gcc -c test.c

4、链接 linking

  • gcc test.o 生成可执行文件

可以一步编译成可执行文件.out

  • gcc test.c

多个文件编译成一个

  • six.c文件
int six(){
  return 6;
}  
  • six.h文件
int six();
  • demo.c文件
#include <stdio.h>
#include "six.h"
#define STRING "hello word\n"

int main(){
  printf("six is %d \n", six());
  printf(STRING);
}          
  • 执行命令
  • gcc -c test demo.c six.c
  • 生成test 可执行文件
  • ./test 输出结果
six is 6 
hello word

Makefile

  • 创建一个Makefile文件
  • gedit Makefile &
test:	demo.c six.c
		gcc -o test demo.c six.c
clean:
		rm test
  • 执行命令 make

  • 会自动执行gcc -o test demo.c six.c

  • 执行 make clean

  • 会自动执行rm test
    存在一个问题,若是其中一个.c文件修改,会重新在编译,很消耗时间

  • 生成的文件:需要的文件

  •   					执行的命令
    
test:	demo.o six.o
		gcc -o test demo.o six.o
demo.o:	demo.c
		gcc -c test.c
six.o: six.c
		gcc -c six.c
clean:
		rm test
		rm *.o
  • 执行命令 `make
  • 输出为
gcc -c demo.c
gcc -c six.c
gcc -o test demo.o six.o

变量引用

CC=gcc
CFLAGS=-c -Wall
all:	test
test:	demo.o six.o
		$(CC) -o test demo.o six.o
demo.o:	demo.c
		$(CC) $(CFLAGS) test.c
six.o: six.c
		$(CC) $(CFLAGS) six.c
clean:
		rm test
		rm *.o
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值