1、GCC
GCC常用的参数:
-o:表示要输出的目标文件
-c:表示仅编译(Compile),不连接(Make)
示例命令:
gcc -c test.c //表示只编译test.c文件,成功时输出目标文件test.o
gcc - c test.c -o test.o //表示只编译test.c文件,成功时输出目标文件test.o
gcc -c test.c -o demo.o //表示只编译test.c文件,成功时输出目标文件demo.o
gcc -o test test.o //将test.o连接成可执行的二进制文件test
gcc -o test test.c //将test.c编译并连接成可执行的二进制文件test
gcc test.c -o test //将test.c编译并连接成可执行的二进制文件test,注意如果
gcc后面没有跟“-c”这个参数,就表示编译并连接
gcc -c test1.c //只编译test1.c,成功时输出目标文件test1.o
gcc -c test2.c //只编译test2.c,成功时输出目标文件test2.o
gcc -o test test.o test2.o //将test1.o和test2.o连接为可执行的二进制文件test
gcc -o test test1.c test2.c //将test1.c和test2.c编译并连接为可执行的二进制文件test
注意:如果你想编译cpp文件,那么请用g++,否则会有类似如下莫名其妙的错误
cc3r3i2U.o(.eh_frame+0x12): undefined reference to `__gxx_personality_v0’
还有一个参数是"-l"参数,与之仅仅相邻的是表示连接时所要的链接库,比如多线程,如果你使用了
pthread_create函数,那么你就应该在编译语句的最后加上"-lpthread",“-l”表示连接,“pthread”表示要连接的库,注意他们在这里要连接在一起写,还有如果你使用了光标库curse,那么就应该在后面加上“-lcurse”,像下面的写法
gcc -o test test1.o test2.o -lpthread -lcurse
2、Makefile的基本语法
一、目标
在Makefile文件里面一定有这样的几行,他们的开始一定是
all: xxxxxxxxxxx
xxxxxxxxxxx //注意这里光标到第二行的时候,一定要使用Tab键,否则编译会提示错误
install: xxxxxxxxxx //注意这里光标到第二行的时候,一定要使用Tab键,否则编译会提示错误
xxxxxxxxx
clean: xxxxxxxx //注意这里光标到第二行的时候,一定要使用Tab键,否则编译会提示错误
xxxxxxxxx
在上面所提到的all、install、clean就是我们所说的目标,make all命令,就告诉make,我们将执行
all所指定的目标
##表示Makefile文件中的注释,下面是Makefile文件的具体内容
all:
@echo you have typed command "make all"
clean:
@echo you have typed command "make clean"
install:
@echo you have typed command "make $@"
#Makefile文件结束
再次提醒大家注意:all,clean,install行要顶格,而所有@echo前要加Tab键来跳个缩进,下面是运行
运行结果:
1
[root@localhost Code]# cat Makefile
2
##表示Makefile的注释,下面是Makefile的内容:
3
4
all:
5
6
@echo you have typed command "make all"
7
8
clean:
9
10
@echo you have typed command "make clean"
11
12
install:
13
14
@echo you have typed command "make $@"
15
16
17
18
19
[root@localhost Code]# make all
20
you have typed command make all
21
[root@localhost Code]# make clean
22
you have typed command make clean
23
[root@localhost Code]# make install
24
you have typed command make install
25
[root@localhost Code]#
26
Makefile文件里有个一个符号$@,其中$表示变量名,其后的要当作变量来解释,$@是Makefile预先定义的一个变量,表示目标命令,比如在上面的文件里属于install目标,那么$@就表示install,同样的,你将make clean目标下面的引号的"make clean"换为"make $@",那么命令make clean的输出是和原来的一样的。
2、依赖
//注意从make doall,可以看出,我们当前的目标是doall,因此$@也代表当前的目标doall,所以它是按顺序执行的
现在我将一些文件修改一下,我们看一下Makefile文件的执行顺序。
1
[root@localhost Code]# cat Makefile
2
##表示Makefile的注释,下面是Makefile的内容:
3
4
all:
5
6
@echo you have typed command "make all"
7
8
clean:
9
10
@echo you have typed command "make clean"
11
12
install:
13
14
@echo you have typed command "make $@"
15
16
doall:all clean install
17
18
@echo you have typed command "make $@"
19
20
21
22
[root@localhost Code]# make doall
23
you have typed command make all
24
you have typed command make clean
25
you have typed command make install
26
you have typed command make doall
27
[root@localhost Code]#
28
注意这里,我把all,clean,install放在doall的后面
可以看到doall目标执行的顺序是,先调用all目标,然后调用clean目标,再调用install目标,最后是调用自己的doall目标,我们称all,install,clean为doall的依赖目标,简称为doall的依赖,你要执行doall目标,你就必须先执行all,install,clean之后,再执行我doall的代码