如何在GCC搜索路径中包含头文件?

Mar*_*ark 51 c++ gcc header

我在示例文件中有以下代码:

#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkGLCanvas.h"
#include "SkGraphics.h"
#include "SkImageEncoder.h"
#include "SkPaint.h"
#include "SkPicture.h"
#include "SkStream.h"
#include "SkWindow.h"
Run Code Online (Sandbox Code Playgroud)

但是,此代码位于/ home/me/development/skia中的各种文件夹中(其中包括core/animator/images/ports/svg /等等).

如何让GCC认识到这条道路?

Tim*_*ert 75

试试gcc -c -I/home/me/development/skia sample.c.看到这里.

  • 很高兴在这里看到这个答案.值得一提的另一点是,当你有许多".c"源文件时,有必要在命令行本身中指定它们中的每一个.您不能只执行-I之类的操作来指定所有源文件都在某个目录中. (2认同)
  • 如果标题与源位于同一目录中,您是否需要特殊的包含?我不能让我的代码以任何方式编译,我不确定问题是什么 (2认同)

小智 25

-I指令完成了这项工作:

gcc -Icore -Ianimator -Iimages -Ianother_dir -Iyet_another_dir my_file.c 
Run Code Online (Sandbox Code Playgroud)


Sco*_* P. 5

当您不控制构建脚本/过程时,使用环境变量有时会更方便。

对于 C 包括使用C_INCLUDE_PATH.

对于 C++ 包括使用CPLUS_INCLUDE_PATH.

有关其他 gcc 环境变量,请参阅此链接

MacOS / Linux 中的示例用法

# `pip install` will automatically run `gcc` using parameters
# specified in the `asyncpg` package (that I do not control)

C_INCLUDE_PATH=/home/scott/.pyenv/versions/3.7.9/include/python3.7m pip install asyncpg
Run Code Online (Sandbox Code Playgroud)

Windows 中的示例用法

set C_INCLUDE_PATH="C:\Users\Scott\.pyenv\versions\3.7.9\include\python3.7m"

pip install asyncpg

# clear the environment variable so it doesn't affect other builds
set C_INCLUDE_PATH=
Run Code Online (Sandbox Code Playgroud)