GCC编译选项,包含的头文件

转自:http://blog.sina.com.cn/s/blog_46d528490100l0n3.html

许多情况下,头文件和源文件会单独存放在不同的目录中。

可以直接在.c文件中利用#include“/path/file.h", 通过指定头文件的路径(可以是绝对路径, 也可以是相对路径)来包含头文件. 但这明显降低了程序的可移植性. 在别的系统环境下编译可能会出现问题.

   所以还是利用"-I"选项指定头文件完整的包含路径.

  针对头文件比较多的情况, 最好把它们统一放在一个目录中, 比如~/project/include. 这样就不需为不同的头文件指定不同的路径. 如果你嫌每次输入这么多选项太麻烦, 你可以通过设置环境变量来添加路径:

  $ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include

  $ export C_INCLUDE_PATH

  $ LIBRART_PATH=/opt/gdbm-1.8.3/lib

  $ export LIBRART_PATH

  可一次指定多个搜索路径,":"用于分隔它们,"."表示当前路径,如:

  $ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include

  $ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

  (可以添加多个路径,路径之间用:相隔,.代表当前目录,若.在最前头,也可省略)

  当然,若想永久地添加这些路径,可以在.bash_profile中添加上述语句.

 

   例如,假设存放源文件的子目录名为./src,而包含文件则放在同层次的其他目录下,如./inc。当我们在./src 目录下进行编译工作时,如何告诉GCC到哪里找头文件呢?方法如下所示:
$ gcc test.c  ,I../inc -o test
  上面的命令告诉GCC包含文件存放在./inc 目录下,在当前目录的上一级。如果在编译时需要的包含文件存放在多个目录下,可以使用多个-I 来指定各个目录:
    包含多个目录: $ gcc test.c  ,I../inc ,I../../inc2 -o test
    这里指出了另一个包含子目录inc2,较之前目录它还要在再上两级才能找到。

 

    C 编译器通过源文件的后缀名来判断是 C 程序还是 C++ 程序。在 Linux 中,C 源文件的后缀名为 .c,而 C++ 源文件的后缀名为 .C 或 .cpp。但是,gcc 命令只能编译 C++ 源文件,而不能自动和 C++ 程序使用的库连接。因此,通常使用 g++ 命令来完成C++ 程序的编译和连接,该程序会自动调用 gcc 实现编译。

    -g 生成调试信息。GNU 调试器可利用该信息。

    -w 不生成任何警告信息。
    -Wall 生成所有警告信息。

    -c 只编译并生成目标文件

the -c option says not to run the linker.Then the output consists of object files output 

by the assembler.

 

-c  Compile or assemble the source files, but do not link.The linking stage simply is not done.

    The ultimate output is in the form of an object file for each source file.

   By default, the object file name for a source file is made by replacing the suffix 

    .c, .i, .s, etc., with .o.

   Unrecognized input files, not requiring compilation or assembly,are ignored.

-S  Stop after the stage of compilation proper; do not assemble.  The output is in the form of 

    an assembler code file for each nonassembler input file specified.

   By default, the assembler file name for a source file is made by replacing the suffix 

    .c, .i, etc., with .s.

   Input files that don\u2019t require compilation are ignored.

-E  Stop after the preprocessing stage; do not run the compiler proper. The output is 

    in the form of preprocessed source code,which is sent to the standard output.

   Input files which don't require preprocessing are ignored.

-v  Print (on standard error output) the commands executed to run the stages of compilation.

    Also print the version number of the compiler driver program and of the preprocessor 

    and the compiler proper.

--version Display the version number and copyrights of the invoked GCC.

-### Like -v except the commands are not executed and all command arguments are quoted.

    This is useful for shell scripts to capture the driver-generated command lines.

-pipe Use pipes rather than temporary files for communication between the various 

    stages of compilation.

    This fails to work on some systems where the assembler is unable to read from a pipe; 

    but the GNU assembler has no trouble.

优化选项: Options That Control Optimization(略)

     

关于编译选项的次序: Order does matter when you use several options of the same kind; for example, if you specify -L more than once, the directories are searched in the order specified.

 

Overall Options
    -c  -S  -E  -o file  -pipe  -pass-exit-codes -x language -v -###
    --help  --target-help  --version

C Language Options
    -ansi  -std=standard  -aux-info filename -fno-asm  -fno-builtin
    -fno-builtin-function -fhosted  -ffreestanding  -fms-extensions
    -trigraphs  -no-integrated-cpp  -traditional  -traditional-cpp
    -fallow-single-precision  -fcond-mismatch -fsigned-bitfields
    -fsigned-char -funsigned-bitfields  -funsigned-char
    -fwritable-strings


下面的是一个调用数学库 libm.a 中 sin 函数的的例子,创建文件calc.c

#include <math.h>

#include <stdio.h>

int main (void){

    double x = 2.0;

    double y = sin (x);

    printf ("The value of sin(2.0) is %f\n", y);

    return 0;

}

尝试单独从该文件生成一个可执行文件将导致一个链接阶段的错误:函数 sin,未在本程序中定义也不在默认库‘libc.a’中;

    gcc -Wall calc.c /usr/lib/libm.a -o calc

为避免在命令行中指定长长的路径,编译器为链接函数库提供了快捷的选项‘-l’。例如,下面的命令 

     $ gcc -Wall calc.c -lm -o calc

与我们上面指定库全路径‘/usr/lib/libm.a’的命令等价。

程序通常要使用很多 -l 选项来指定要链接的数学库,图形库,网络库等。

1)在实现共享库时,要将源文件编译为相对地址编码的格式。

2)Gcc选项 -fpic是实现1)中要求的选项。pic是position independent code的缩写。如:gcc -c -fpic component1.c component2.c

3)在链接时定位共享库:用-L指定绝对路径,也可以是用-l指定相对路径。

    默认搜索路径是/lib和/usr/lib

4) 在运行时,应用程序的搜索路径包括:环境变量LD_LIBRARY_PATH指定的路径、/etc/ld.so.cache中的共享库(由 ldconfig生成)、/lib和/usr/lib。另外还有一个环境变量LD_PRELOAD,在这里定义的共享库会比任何前述路径优先搜索。

 

gdb调试时:查看数据

        print  variable        查看变量

        print  *array@len      查看数组(array是数组指针,len是需要数据长度)

        可以通过添加参数来设置输出格式:

            /x 按十六进制格式显示变量。

            /d 按十进制格式显示变量。

            /u 按十六进制格式显示无符号整型。

            /o 按八进制格式显示变量。

            /t 按二进制格式显示变量。

            /a 按十六进制格式显示变量。

            /c 按字符格式显示变量。

            /f 按浮点数格式显示变量。

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们使用gcc编译代码时,如果出现了头文件报错的情况,一般有以下几种可能性: 1. 头文件路径错误:编译器无法找到指定的头文件。这可能是因为我们在include语句中指定的路径有误,或者头文件没有正确地放置在指定路径下。我们应该检查include语句中的路径是否正确,并确保头文件存在于指定的路径中。 2. 头文件缺失:有时我们可能会忽略了某个必要的头文件。如果我们在代码中使用了某个库或其他文件的函数或类型,但是忘记了包含相应的头文件编译器会报错。解决方法是查找并包含所需的头文件。 3. 头文件名字拼写错误:如果我们在include语句中拼写错误,或者文件名与实际头文件不匹配,编译器会抱错。我们应该仔细检查include语句中的文件名拼写,并与实际的头文件进行比对。 4. 头文件冲突:当我们在不同的库或模块中使用相同的头文件名时,可能会出现头文件冲突。这就意味着编译器无法确定应该使用哪个头文件。为了解决这个问题,我们可以使用全局的唯一命名约定或者重命名冲突的头文件。 5. 编译选项错误:有时使用错误的编译选项也会导致头文件报错。我们应该确保使用了正确的编译选项,并查看编译器文档以了解正确的选项和用法。 当头文件报错时,我们应该仔细检查上述可能性,并根据实际情况进行排除和解决。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值