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 "".

Difference between #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.

这一切都有点简单,但应该会给您一个基本的概念.

Details on <>/""-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或.

C++相关问答推荐

由于未签名int导致的运行时错误"

如何用C(使用两个s补数算术的32位程序)计算

使用SWI—Prolog的qsave_program生成二进制文件有什么好处?'

无效使用未定义类型'structsquare'?

为什么下面的C代码会进入无限循环?

C中出现分段错误后关闭文件

手动矢量化性能差异较大

轮询libusb_pollfd struct 列表的正确方式是什么?

如何在下面的C代码中正确管理内存?

关于scanf()和空格的问题

如何在C-函数中混合使用C代码和ASM?

将变量或参数打包到 struct /联合中是否会带来意想不到的性能损失?

如何在VSCode中创建和使用我自己的C库?

将数字的每一位数平方,并使用C将它们连接为一个数字(程序不能正确处理0)

int * 指向int的哪个字节?

';malloc():损坏的顶部大小';分配超过20万整数后

如何在Rust中处理C的longjmp情况?

C struct 中的冒泡排序

'printf("%s", user_input)' 危险吗?

free后内存泄漏?