GCC / G ++:"没有这样的文件或目录" [英] gcc/g++: "No such file or directory"

查看:225
本文介绍了GCC / G ++:"没有这样的文件或目录"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

G ++ 给我出错的形式:

  foo.cc:<line>:&LT;列计算值:致命错误:其中;酒吧计算值:没有这样的文件或目录
编译终止。

与编译C-程序时它是在同一个 GCC

这是为什么?


请注意:这个问题已经被问过很多次,但每次它是具体到提问者的情况。这个问题的目的是以有其他人可以停课时重复,一劳永逸的问题;一个的常见问题解答


解决方案

您的编译器只是试图编译文件名为 foo.cc 。一旦击中行号,编译器发现:

 的#include栏

 的#include&LT;酒吧和GT;

然后,编译器试图找到该文件。为此,它使用一组目录中查找到的,但这种集中的,没有文件。有关这些版本之间的差别的解释包括声明一下<一个href=\"http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename\">here.

如何告诉编译器在哪里找到它

G ++ 有一个选项 -I 。它可以让你添加包括在命令行中搜索路径。试想一下,你的文件 frobnicate ,相对于 foo.cc <命名的文件夹中/ code>(假定您从其中 foo.cc 所在的目录编译):

  G ++ -Ifrobnicate foo.cc

您可以添加更多的包括路径;每次你给是相对于当前目录。微软的编译器有一个相关的选项 / I ,在相同的方式工作,或在Visual Studio中,文件夹可以在项目的属性页中设置,在配置属性> C / C ++ - >常规 - >附加包含目录。

现在假设您有在不同的文件夹栏的多个版本,因为:


  // A /条
#包括LT&;串GT;
性病::字符串,(){返回A /条; }


  // B /酒吧
#包括LT&;串GT;
性病::字符串,(){返回B /酒吧; }


  // C /条
#包括LT&;串GT;
性病::字符串,(){返回的C /条; }


  // foo.cc
#包括栏
#包括LT&;&iostream的GT;诠释主(){
    性病::法院LT&;&LT;其中()&所述;&下;的std :: ENDL;
}


优先的#include栏是最左边的:

  $ G ++ -IA -IB -IC foo.cc
$ ./a.out
一间酒吧

正如你所见,当编译器开始寻找通过 A / B / C / ,它停在第一或最左边的打击。

这是两种形式的真实,包括&LT;&GT; incude

的区别的#include&LT;酒吧和GT; 的#include栏

一般情况下,的#include&LT; XXX&GT; 使得它看起来到系统文件夹第一,的#includeXXX使得它看起来到当前或自定义文件夹第一。

例如:

想象一下,你在你的项目文件夹中的以下文件:

 列表
main.cc

main.cc

 的#include排行榜
....

对于这一点,你的编译器的#include 文件列表在项目文件夹,因为它目前编译 main.cc 并没有该文件列表在当前文件夹中。

但随着 main.cc

 的#include&LT;列表&gt;
....

然后 G ++ main.cc ,编译器会寻找到系统文件夹的第一个,因为&LT;列表&gt; 是一个标准的头,它会的#include 名为列表随你的C ++平台的部分文件标准库。

这是所有有点简化,但应该给你的基本思想。

&LT

详细信息;&GT; / -priorities和 -I

按照 GCC-文档,为的优先级包括: &LT;&GT; 是,正常的Unix系统,如下所示:

 的/ usr /本地/包括
 LIBDIR / GCC /目标/版本/包括
 在/ usr /目标/包括
 / usr / include目录


  

有关C ++程序,它也将看在/ usr / include目录/ C ++ /版,第一位。在上面的,目标是系统GCC的规范名称配置为编译code代表; [...]。


该文件还指出:


  

您可以添加到这个列表与-Idir命令行选项。所有-I指定的目录出现在左到右的顺序,在默认的目录 。 DIR时已默认搜索唯一的例外是。在这种情况下,选项将被忽略并为系统目录搜索顺序保持不变。


要继续我们的#包括LT&;列表&gt; /#包括目录例如(同一code):

  G ++ -I。 main.cc

 #包括LT&;列表&gt;
INT的main(){性病::名单&LT; INT&GT;升; }

而事实上,在 -I 优先文件夹在系统包括,我们得到一个编译器错误

g++ gives me errors of the form:

foo.cc:<line>:<column>: fatal error: <bar>: No such file or directory
compilation terminated.

It is the same when compiling C-programs with 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.

解决方案

Your compiler just tried to compile the file named foo.cc. Upon hitting line number line, the compiler finds:

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

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

Now imagine you have multiple version of bar in different folders, given:


// 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:

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

This is true of both forms, include <> and incude "".

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

E.g.:

Imagine you have the following files in your project folder:

list
main.cc

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

But with 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.

This is all a bit simplified, but should give you the basic idea.

Details on <>/""-priorities and -I

According to the gcc-documentation, the priority for include <> is, on a "normal Unix system", as follows:

 /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; [...].

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.

To continue our #include<list> / #include"list" example (same code):

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.

这篇关于GCC / G ++:&QUOT;没有这样的文件或目录&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆