g++
给我以下形式的错误:
foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory
compilation terminated.
用 gcc
编译 C 程序时也是如此。
这是为什么?
请注意: 这个问题以前被问过很多次,但每次都是针对提问者的情况。这个问题的目的是 提出一个问题,其他人可以一劳永逸地关闭作为重复的问题; _常见问题解答_。
原文由 Sebastian Mach 发布,翻译遵循 CC BY-SA 4.0 许可协议
您的编译器刚刚尝试编译名为
foo.cc
的文件。在点击行号line
时,编译器发现:或者
然后编译器尝试找到该文件。为此,它使用一组目录进行查看,但在这组目录中,没有文件
bar
。有关 include 语句版本之间差异的解释,请参见 此处。如何告诉编译器在哪里找到它
g++
有一个选项-I
。它允许您将包含搜索路径添加到命令行。 Imagine that your filebar
is in a folder namedfrobnicate
, relative tofoo.cc
(assume you are compiling from the directory wherefoo.cc
is位于):您可以添加更多包含路径;你给的每一个都是相对于当前目录的。 Microsoft 的编译器有一个相关选项
/I
工作方式相同,或者在 Visual Studio 中,可以在项目的属性页中设置文件夹,在配置属性->C/C++->常规- >其他包含目录。现在假设您在不同的文件夹中有多个版本的
bar
,给定:#include "bar"
的优先级是最左边的:如您所见,当编译器开始查看
A/
、B/
和C/
时,它停止在第一个或最左边的命中。这两种形式都是如此,
include <>
和incude ""
。\#include <bar>
和\#include "bar"
之间的区别通常,
#include <xxx>
使其首先查看系统文件夹,#include "xxx"
使其首先查看当前或自定义文件夹。例如:
假设您的项目文件夹中有以下文件:
与
main.cc
:For this, your compiler will
#include
the filelist
in your project folder, because it currentlymain.cc
and there is that filelist
在当前文件夹中。但是使用
main.cc
:and then
g++ main.cc
, your compiler will look into the system folders first, and because<list>
is a standard header, it will#include
the file namedlist
作为标准库的一部分随您的 C++ 平台一起提供。这有点简化,但应该给你基本的想法。
详细信息
<>
/""
-priorities 和-I
根据 gcc-documentation ,
include <>
在“普通 Unix 系统”上的优先级如下:该文档还指出:
继续我们的
#include<list> / #include"list"
示例(相同的代码):和
事实上,
-I.
优先于系统包含的文件夹.
并且我们得到一个编译器错误。