.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