C语言 如何将目录添加到 C 头文件包含路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4825652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:38:19  来源:igfitidea点击:

How do I add a directory to C header include path?

cmacos

提问by wdkrnls

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:

我在为本身依赖于 pcre.h 的程序安装依赖项时遇到问题。我将它安装到 /opt/local/include,但 C 编译器没有看到它,因此给了我:

error: pcre.h: No such file or directory

错误:pcre.h:没有那个文件或目录

I have confirmed this by writing a hello world program that tries to include it:

我已经通过编写一个试图包含它的 hello world 程序来确认这一点:

#include <pcre.h>
#include <stdio.h>

int main(void)
{
    printf("hello, world\n");
    return 0;
}

This also gives the error unless I specify the path as </opt/local/include/pcre.h>.

除非我将路径指定为</opt/local/include/pcre.h>.

I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATHenvironment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.

我希望 C 编译器在默认情况下找到它,但我不知道这是在哪里配置的。选项卡完成没有显示任何HEADER_PATH环境变量,我找不到任何类似的东西不是特定于XCode 的。然而,我正在使用 Mac OSX Snow Leopard 的机会有所不同。

回答by R.. GitHub STOP HELPING ICE

Use -I /opt/local/includeon the command line or C_INCLUDE_PATH=/opt/local/includein the environment.

使用-I /opt/local/include在命令行或者C_INCLUDE_PATH=/opt/local/include在环境中。

回答by a paid nerd

Use the pcre-configutility to get the right flags:

使用该pcre-config实用程序获取正确的标志:

$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include

If you're compiling via the command line,

如果您通过命令行编译,

$ gcc -Wall -g `pcre-config --libs --cflags` main.c