C++ gcc/g++:“没有那个文件或目录”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12919081/
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
gcc/g++: "No such file or directory"
提问by Sebastian Mach
g++
gives me errors of the form:
g++
给我的形式错误:
foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory
compilation terminated.
It is the same when compiling C-programs with gcc
.
使用gcc
.
Why is that?
这是为什么?
Please note:This question has been asked many times before, but each time it was specific to the askers situation. This question's purpose is to have a question that others can be closed as duplicates of, once and for all; a FAQ.
请注意:这个问题以前被问过很多次,但每次都是针对提问者的情况。这个问题的目的是提出一个问题,其他人可以一劳永逸地作为 的副本关闭;一个常见问题。
回答by Sebastian Mach
Your compiler just tried to compile the file named foo.cc
. Upon hitting line number line
, the compiler finds:
您的编译器只是尝试编译名为foo.cc
. 点击 line number 后line
,编译器发现:
#include "bar"
or
或者
#include <bar>
The compiler then tries to find that file. For this, it uses a set of directories to look into, but within this set, there is no file bar
. For an explanation of the difference between the versions of the include statement look here.
然后编译器尝试找到该文件。为此,它使用一组目录来查看,但在该组中,没有 file bar
。有关 include 语句版本之间差异的说明,请查看此处。
How to tell the compiler where to find it
如何告诉编译器在哪里找到它
g++
has an option -I
. It lets you add include search paths to the command line. Imagine that your file bar
is in a folder named frobnicate
, relative to foo.cc
(assume you are compiling from the directory where foo.cc
is located):
g++
有一个选项-I
。它允许您向命令行添加包含搜索路径。想象一下,您的文件bar
位于一个名为 的文件夹中frobnicate
,相对于foo.cc
(假设您正在从所在目录编译foo.cc
):
g++ -Ifrobnicate foo.cc
You can add more include-paths; each you give is relative to the current directory. Microsoft's compiler has a correlating option /I
that works in the same way, or in Visual Studio, the folders can be set in the Property Pages of the Project, under Configuration Properties->C/C++->General->Additional Include Directories.
您可以添加更多包含路径;你给的每一个都是相对于当前目录的。Microsoft 的编译器有一个相关选项/I
,其工作方式相同,或者在 Visual Studio 中,可以在项目的属性页中设置文件夹,在 Configuration Properties->C/C++->General->Additional Include Directories 下。
Now imagine you have multiple version of bar
in different folders, given:
现在想象你bar
在不同的文件夹中有多个版本,给定:
// A/bar
#include<string>
std::string which() { return "A/bar"; }
// B/bar
#include<string>
std::string which() { return "B/bar"; }
// C/bar
#include<string>
std::string which() { return "C/bar"; }
// foo.cc
#include "bar"
#include <iostream>
int main () {
std::cout << which() << std::endl;
}
The priority with #include "bar"
is leftmost:
优先级在#include "bar"
最左边:
$ g++ -IA -IB -IC foo.cc
$ ./a.out
A/bar
As you see, when the compiler started looking through A/
, B/
and C/
, it stopped at the first or leftmost hit.
如您所见,当编译器开始查看A/
,B/
和 时C/
,它在第一个或最左边的命中处停止。
This is true of both forms, include <>
and incude ""
.
两种形式都是如此,include <>
和incude ""
。
Difference between #include <bar>
and #include "bar"
#include <bar>
和之间的区别#include "bar"
Usually, the #include <xxx>
makes it look into system folders first, the #include "xxx"
makes it look into the current or custom folders first.
通常,#include <xxx>
它首先查看系统文件夹,然后#include "xxx"
首先查看当前或自定义文件夹。
E.g.:
例如:
Imagine you have the following files in your project folder:
假设您的项目文件夹中有以下文件:
list
main.cc
with main.cc
:
与main.cc
:
#include "list"
....
For this, your compiler will #include
the file list
in your project folder, because it currently compiles main.cc
and there is that file list
in the current folder.
为此,您的编译器将在您的项目文件夹中使用#include
该文件list
,因为它当前正在编译main.cc
并且list
当前文件夹中有该文件。
But with main.cc
:
但是main.cc
:
#include <list>
....
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 named list
that comes with your C++ platform as part of the standard library.
然后g++ main.cc
,您的编译器将首先查看系统文件夹,因为<list>
它是标准头文件,所以它会将您的 C++ 平台附带的#include
命名文件list
作为标准库的一部分。
This is all a bit simplified, but should give you the basic idea.
这有点简化,但应该给你基本的想法。
Details on <>
/""
-priorities and -I
<>
/ -""
优先级的详细信息和-I
According to the gcc-documentation, the priority for include <>
is, on a "normal Unix system", as follows:
根据gcc-documentation,在include <>
“普通 Unix 系统”上的优先级如下:
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
For C++ programs, it will also look in /usr/include/c++/version, first. In the above, target is the canonical name of the system GCC was configured to compile code for; [...].
对于 C++ 程序,它还会首先在 /usr/include/c++/version 中查找。在上面,target 是 GCC 被配置为编译代码的系统的规范名称;[...]。
The documentation also states:
该文件还指出:
You can add to this list with the -Idir command line option. All the directories named by -I are searched, in left-to-right order, before the default directories. The only exception is when dir is already searched by default. In this case, the option is ignored and the search order for system directories remains unchanged.
您可以使用 -Idir 命令行选项添加到此列表中。在默认目录之前,按从左到右的顺序搜索所有由 -I 命名的目录。唯一的例外是默认情况下已经搜索了 dir 。在这种情况下,该选项将被忽略,系统目录的搜索顺序保持不变。
To continue our #include<list> / #include"list"
example (same code):
继续我们的#include<list> / #include"list"
例子(相同的代码):
g++ -I. main.cc
and
和
#include<list>
int main () { std::list<int> l; }
and indeed, the -I.
prioritizes the folder .
over the system includes and we get a compiler error.
事实上,-I.
文件夹优先于.
系统包含,我们得到一个编译器错误。