当前位置: 首页 >> 程序设计 >> An Introduction to GCC 学习笔记:
 

An Introduction to GCC 学习笔记:

作者:吴学军 2004-8      来源:本站原创     发表时间:2006-03-11     浏览次数:      字号:    

内容摘要 注:转载时请注明出处和作者(吴学军 wu_xuejun@hotmail.com) 来源于GNU文档学习手册

 
前三章:
要点
 
1。强烈推荐使用-Wall选项;
   注:Warning 的显示格式:    file:line-number:message
 
2。编译选项
-o 文件名:直接产生可执行文件
-c 文件名:只编译为.o的库文件,不链接  (.c => .o )
 
在将多个.o连接为一个可执行文件时,仅使用-o指定可执行文件名即可,不需要使用
-Wall选项,因为链接是一个明确的过程(unambiguous),只有successed或者fail
两种结果。
 
incidentally [InsI5dentElI] adv. 附带地, 顺便提及(学单词一个:)
 
分两步处理:
每个.c文件编译为.o,然后通过linker连接为一个可执行文件;
这样每次只编译修改过的.c文件,可以缩短整体编译时间:
An object file contains machine code where any references to the memory addresses of functions(orvariables)in other files are left undefined.This allows source files to be compiled without direct reference to each other.The linker fills in these missing addresses when it produces the executable.
 
3。连接解析顺序:
在类unix的系统中,编译器和链接器是通过在命令行中从左向右的顺序检索各个文件。这就意味着:
含有函数定义的.o文件要放在那些调用该函数的.o文件的右边。
This means that the object file which contains the definition of a function should appear after any files which call that function.
 
4。库的连接
A library is a collection of precompiled object files which can be linked into programs. Libraries are typically stored in special archive files with the extension‘.a’, referred to as static libraries.They are created from object files with a separate tool, the GNU archiver ar, and used by the linker to resolve references to functions at compile-time.
 
-l参数:指定欲链接的库名称
比如我们相连接标准库目录(/usr/lib, /lib)下的数学库libm.a,那么我们可以这样指定:
-lm (具体见下面的解释)
 
In general, the compiler option ‘-lNAME’ will attempt to link object
files with a library file ‘libNAME.a’ in the standard library directories.
Additional directories can specified with command-line options and environment
variables, to be discussed shortly. A large program will typically
use many ‘-l’ options to link libraries such as the math library, graphics
libraries and networking libraries.
 
5。其他的一些编译选项
5.1  -I,-L
假如程序中使用的头文件路经和链接时使用的.a路径不是缺省的系统路径(如下)
By default, gcc searches the following directories for header files:
/usr/local/include/
/usr/include/
and the following directories for libraries:
/usr/local/lib/
/usr/lib/
 
则,需要利用 -I 来指定头文件的include路径
    需要利用 -L 来指定.a文件的库文件路径
(注:不要在文件include中使用绝对路径来避免这个问题,因为这样对移植后的编译不利)
 
同时,也可以利用环境变量来解决搜索路径的问题(Environment variables)
对于include 问题:
Additional directories can be added to the include path using the environment
variable C_INCLUDE_PATH (for C header files) or CPLUS_INCLUDE_PATH (for C++ header files).
举例:
$ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include
$ export C_INCLUDE_PATH
这样这个头文件路经就被添加到了环境变量中了(利用export使得其他模块也可见该变量)
 
同样的添加库文件路经:
additional directories can be added to the link path using the environment variable LIBRARY_PATH.
举例:
$ LIBRARY_PATH=/opt/gdbm-1.8.3/lib
$ export LIBRARY_PATH
 
那么,想指定多个路径可以采用下面的格式:
dir1:dir2:dir3:......
其中每一个单独的路径用 : 隔开;
一个点  . 表示当前路径
举例:
$ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include (含三个路径)
$ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib (含三个路径)
5.2 静态库和共享库(static libraries and shared libraries.)
库有两种方式存在,
Static libraries are the ‘.a’ files seen earlier.
Shared libraries are handled with a more advanced form of linking, which makes the executable file smaller. They use the extension ‘.so’,which stands for shared object.
 
[ 短语:in preference to 优先于... ]
 
.so的文件是动态链接并执行的。
当我们指定一个路径下的库文件名时,假如此时同时存在xxx.a和xxx.so的两个库形式,那么
优先选择.so链接。(共享库优先
 
当执行函数动态链接.so时,如果此文件不在缺省目录下‘/usr/local/lib’ and ‘/usr/lib’.
那么就需要指定环境变量LD_LIBRARY_PATH (方式和上面include,lib path相同)
 
假如现在需要在已有的环境变量上添加新的路径名,则采用如下方式:
LD_LIBRARY_PATH=NEWDIRS:$LD_LIBRARY_PATH.(newdirs是新的路径串)
 
(注:gnu系统可以自动添加在 /etc/ld.so.conf文件中来实现环境变量的设置)
 
-static
使用该选项可以强制编译器生成静态库文件
 
6. C语言标准
-ansi
the compiler option‘-ansi’ disables those GNU extensions which conflict with the ANSI/ISO standard.
 
-pedantic
The command-line option ‘-pedantic’ in combination with ‘-ansi’ will
cause gcc to reject all GNU C extensions, not just those that are incompatible
with the ANSI/ISO standard. This helps you to write portable
programs which follow the ANSI/ISO standard.
 
-std
The specific language standard used by GCC can be controlled with the‘-std’ option.
包含有:
‘-std=c89’ or ‘-std=iso9899:1990’-- The original ANSI/ISO C language standard
‘-std=iso9899:199409’-- The ISO C language standard with ISO Amendment 1, published
                          in 1994.
‘-std=c99’ or ‘-std=iso9899:1999’-- The revised ISO C language standard,
                                         published in 1999 (ISO/IEC9899:1999).
‘-std=gnu89’ and ‘-std=gnu99’. -- for GNU extensions
 
 
7.关于warning -Wall的具体选项
以下的一些编译选项都是包含在Wall中的一些具体项目,可以单独使用。
‘-Wcomment’  This option warns about nested comments.
‘-Wformat’   This option warns about the incorrect use of format strings in functions
               such as printf and scanf, where the format specifier does not agree with the  
               type of the corresponding function argument.
‘-Wunused’   This option warns about unused variables.
‘-Wimplicit’ This option warns about any functions that are used without being declared.
‘-Wreturn-type’ This option warns about functions that are defined without a return
                  type but not declared void. It also catches empty return statements in 
                  functions that are not declared void.
 
8. 其他的一些warn编译选项(不包含在Wall中的)
‘-W’     This is a general option similar to ‘-Wall’ which warns about a selection of 
           common programming errors, such as functions which can return without a value 
           (also known as “falling off the end of the function body”), and comparisons
           between signed and unsigned values.
        &

[1] [2] [3] [4]

责任编辑 webmaster

 
 
 
 
 
评论更多>>
 
 
 
发表
 
姓名: QQ:
性别: MSN:
E-mail: 主页:
评分: 1 2 3 4 5
评论内容:
验证码:
  
  • 请遵守《互联网电子公告服务管理规定》及中华人民共和国其他各项有关法律法规。
  • 严禁发表危害国家安全、损害国家利益、破坏民族团结、破坏国家宗教政策、破坏社会稳定、侮辱、诽谤、教唆、淫秽等内容的评论 。
  • 用户需对自己在使用本站服务过程中的行为承担法律责任(直接或间接导致的)。
  • 本站管理员有权保留或删除评论内容。
  • 评论内容只代表网友个人观点,与本网站立场无关。
  •