【问题标题】:How to add a new header file location in the compiler如何在编译器中添加新的头文件位置
【发布时间】:2017-07-30 18:29:00
【问题描述】:

各种资源都提到了 gcc 的 -I 选项,但没有提到它的语法。我想添加一个默认目录中不存在的头文件,编译器会考虑在编译时添加头文件。我怎样才能实现它?

【问题讨论】:

  • man gcc,搜索-I

标签: c linux gcc header-files


【解决方案1】:

正如手册页所说,直接在参数后面加上路径。

gcc ... -I/path/to/headers ...

【讨论】:

  • 这样 - gcc tst2.c -o tst2 -I/usr/src/linux-headers-3.13.0-32/include/linux
  • 将位置参数放在要编译的源代码之前。
  • this - gcc -I/usr/src/linux-headers-3.13.0-32/include/linux tst2.c -o tst2
【解决方案2】:

您可以使用-I 选项:

gcc -o foobar -I/path/to/headers -I/path/to/other foobar.c

您还可以使用C_INCLUDE_PATH 环境变量。您可以在您的 makefile 中进行设置:

C_INCLUDE_PATH = /path/to/headers:/path/to/other

foobar: foobar.c
    gcc -o foobar foobar.c

【讨论】:

    【解决方案3】:

    您可以在命令行中添加-I 选项来告诉编译器在那里查找头文件。如果你在 include/ 目录中有头文件,那么这个命令应该适合你。

    gcc -Iinclude/
    

    -I 编译器选项和目录位置之间不应有任何空格。

    如果您正在使用 makefile,您可以在 makefile 的 CFLAGS 宏中包含此选项。

    CFLAGS = -Iinclude/ -c -Wall
    

    您可以使用#include "../include/header.h" 包含头文件。

    看看这个answer

    【讨论】:

      猜你喜欢
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多