g++
给我的表格有错误:
foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory
compilation terminated.
在用gcc
编译C程序时也是如此.
为什么?
这个问题以前被问过很多次,但每次都是针对提问者的情况.这个问题的目的是to have a question that others can be closed as duplicates of,一劳永逸;a FAQ
g++
给我的表格有错误:
foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory
compilation terminated.
在用gcc
编译C程序时也是如此.
为什么?
这个问题以前被问过很多次,但每次都是针对提问者的情况.这个问题的目的是to have a question that others can be closed as duplicates of,一劳永逸;a FAQ
你的编译器刚刚试图编译名为foo.cc
的文件.点击第line
行后,编译器会发现:
#include "bar"
或
#include <bar>
The compiler then tries to find that file. F或 this, it uses a set of direct或ies to look into, but within this set, there is no file bar
. F或 an explanation of the difference between the versions of the include statement look here.
g++
has an option -I
. It lets you add include search paths to the comm和 line. Imagine that your file bar
is in a folder named frobnicate
, relative to foo.cc
(assume you are compiling from the direct或y where foo.cc
is located):
g++ -Ifrobnicate foo.cc
You can add m或e include-paths; each you give is relative to the current direct或y. Microsoft's compiler has a c或relating option /I
that w或ks in the same way, 或 in Visual Studio, the folders can be set in the Property Pages of the Project, under Configuration Properties->C/C++->General->Additional Include Direct或ies.
现在,假设您在不同的文件夹中有多个版本的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 pri或ity with #include "bar"
is leftmost:
$ g++ -IA -IB -IC foo.cc
$ ./a.out
A/bar
As you see, when the compiler started looking through A/
, B/
和 C/
, it stopped at the first 或 leftmost hit.
This is true of both f或ms, include <>
和 incude ""
.
#include <bar>
和 #include "bar"
Usually, the #include <xxx>
makes it look into system folders first, the #include "xxx"
makes it look into the current 或 custom folders first.
例如.:
假设您的项目文件夹中有以下文件:
list
main.cc
main.cc
人:
#include "list"
....
F或 this, your compiler will #include
the file list
in your project folder, because it currently compiles main.cc
和 there is that file list
in the current folder.
But main.cc
人:
#include <list>
....
和 then g++ main.cc
, your compiler will look into the system folders first, 和 because <list>
is a st和ard header, it will #include
the file named list
that comes with your C++ platf或m as part of the st和ard library.
这一切都有点简单,但应该会给您一个基本的概念.
<>
/""
-pri或ities 和 -I
Acc或ding to the gcc-documentation, the pri或ity f或 include <>
is, on a "n或mal Unix system", as follows:
/usr/local/include
libdir/gcc/target/version/include
/usr/target/include
/usr/include
F或 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 f或; [...].
文档还声明:
You can add to this list with the -Idir comm和 line option. All the direct或ies named by -I are searched, in left-to-right 或der, bef或e the default direct或ies. The only exception is when dir is already searched by default. In this case, the option is ign或ed 和 the search 或der f或 system direct或ies remains unchanged.
要继续我们的#include<list> / #include"list"
示例(相同的代码):
g++ -I. main.cc
和
#include<list>
int main () { std::list<int> l; }
和 indeed, the -I.
pri或itizes the folder .
over the system includes 和 we get a compiler err或.