- background
GCC is a programming tool and it is more than that, GCC is part of GNU project,
first realise was made in 1987, in 1992 add the ability to compile c++
gcc was first short for GNU c compiler , and now GNU compiler collection, including fortran ada java objective-c - how to use
1 gcc test.c -o test
test.c 是源代码 test 为输出程序,没有-o test 的话会生成 默认生成 名为test.out的程序
2 options
-Wall 输出绝大多数错误信息
-c 只是编译,生成.o文件为止 不用使用 -o来指定名称, 之后可以使用 gcc test.o -o test 来生成程序
-v 编译的详细记录,可以用来找编译器出现的错误
3 多文件编译和链接
如果我们在程序中调用了其他文件(这个很常见,因为把程序分成多个文件可以带来莫大的好处,例如当需要修改其中的一段代码时候,只需将包含这段代码的文件重新编译即可而不用讲所有代码重新编译一次),gcc是按照从左到右的顺序,如main.c 调用了 hello.c 则应该使用gcc main.c hello.c这样,写反的话可能会出错。链接过程也是从左到右的顺序。
4 使用其他库和头文件
linux下的archive files 以.a结束, 可以使用ar command来生成archive files
standard system libraries 保存在 \usr\lib\ 和 \lib 如 对应于 \usr\include\math.h 的\usr\lib\ libm.a
如在程序中使用了math下面的sqrt函数,则应该使用 gcc -Wall test.c /usr/lib/libm.a -o test (用 gcc -Wall test.c -lm-o test来简写)
默认情况下gcc会在/usr/lcoal/include/ 和 /usr/include/下搜索头文件(前一个头文件有优先性)包含其他路径的时候则需要使用-I 和-Loptionns,当然可以使用环境变量来省去书写的麻烦,C_INCLUDE_PATH (for c header)CPLUS_INCLUDE_PATH (for c++ header) 此时只用在command line 里面敲C_INCLUDE_PATH = pathname export C_INCLUDE_PATH 即可,多个path用":"隔开
5 shared library vs static library
static 库以.a为结尾 shared 库以.so为结尾,前者为静态,后者为动态,可以使用LD_LIBRARY_PATH来省去书写的麻烦可以使用-static来强制使用静态链接
introduction to gcc
最新推荐文章于 2021-06-27 10:19:40 发布