main.cc:5:30: 致命错误: folder/file.h: 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21408415/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:38:04  来源:igfitidea点击:

main.cc:5:30: fatal error: folder/file.h: No such file or directory

c++ubuntumakefile

提问by razorsyntax

When I use type makeinto Ubuntu terminal I get:

当我make在 Ubuntu 终端中使用 type 时,我得到:

main.cc:5:30: fatal error: folder/file.h: No such file or directory

The folder exists in the working directory and the file.h exists in the specified folder.

该文件夹存在于工作目录中,file.h 存在于指定文件夹中。

When I type lsit lists the folder and file in my working directory as well.

当我输入时,ls它也会列出我工作目录中的文件夹和文件。

Oddly enough, when I open it in geany and ask it to find the file in the

奇怪的是,当我在 geany 中打开它并要求它在

#include <folder/file.h>

it finds it without issue but when it builds it I get the error.

它发现它没有问题,但是当它构建它时我收到错误。

Is there a flag I need to set so it so it includes the folder? If so, what would that look like exactly?

是否有我需要设置的标志以便它包含文件夹?如果是这样,那会是什么样子?

回答by MadScientist

This depends a bit on your C compiler, but "typically" when you include a file using the < ... >syntax the compiler will only look for those header files in directories you have specified on the command line with the -Iflag, plus various built-in system directories.

这在一定程度上取决于您的 C 编译器,但是“通常”当您使用该< ... >语法包含一个文件时,编译器只会在您在命令行中使用该-I标志指定的目录以及各种内置系统目录中查找那些头文件.

Notably, it usually will notlook in the current working directory, unless you explicitly add -I.to the compile line.

值得注意的是,它通常不会在当前工作目录中查找,除非您明确添加-I.到编译行。

Alternatively, if you use the " ... "form of #include, then it will look in the current working directory as well.

或者,如果您使用 的" ... "形式#include,那么它也会在当前工作目录中查找。

So, either switch to #include "folder/file.h", or else add -I.to your compile line.

因此,要么切换到#include "folder/file.h",要么添加-I.到您的编译行。

回答by rubenvb

You need to use quotes instead of <>for the include, this makes it that the compiler searches in the source file's directory first:

您需要使用引号而不是<>包含,这使得编译器首先在源文件的目录中搜索:

#include "folder/file.h"

Alternatively explicitly add the current directory to your include paths

或者,将当前目录显式添加到包含路径中

g++ c -I. main.cc

回答by How Chen

Regarding you question, I think first you need tell the compiler, where is you head file, since you write your code as:

关于您的问题,我认为首先您需要告诉编译器,您的头文件在哪里,因为您将代码编写为:

#include <folder/file.h>

I assumed that you store your file.h in $you/include/path/folder, therefore, you need pass -I ${your/include/path/}to compiler like:

我假设您将 file.h 存储在 中$you/include/path/folder,因此,您需要传递-I ${your/include/path/}给编译器,例如:

gcc -I${your/include/path/} ...

be aware, you specified in your code the include as <dir/file.h>, I think this kinds of define show following idea:

请注意,您在代码中指定了 include as <dir/file.h>,我认为这种定义显示了以下想法:

$main_include_path
|
+----folder1
|
+----folder2
|
.
.
.----folderN

Then, you can ONLY write makefileto specified the include path to it father

然后,您只能写入makefile指定其父的包含路径