C++ 致命错误:字符串:没有这样的文件或目录编译终止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9126997/
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
fatal error: string: No such file or directory compilation terminated
提问by Muthu Ganapathy Nathan
I 've been struggling for more than 3 hours but cannot find a solution. A simple helloworld program is running nicely and I can get the output,
我已经挣扎了 3 个多小时,但找不到解决方案。一个简单的 helloworld 程序运行良好,我可以得到输出,
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
cout<<"hello world";
}
But for the My Sudoku Project I have the following user defined header files, SudokuSolver.h, Matrix.h, Cell.h, Pos.h, Error.h, Exception.h and their corresponding .cpp files.
And
ExampleProgram.cpp
uses these header files, to solve a Sudoku.
但是对于我的数独项目,我有以下用户定义的头文件:SudokuSolver.h、Matrix.h、Cell.h、Pos.h、Error.h、Exception.h 及其相应的 .cpp 文件。
并
ExampleProgram.cpp
使用这些头文件,来解决数独。
(All the .h and .cpp files are in a same folder.)
(所有 .h 和 .cpp 文件都在同一个文件夹中。)
I have included using namespace std;
and I included the string as #include <string>
.
in each header file, wherever I used string.h. But even though I am getting the fatal error: string: No such file or directory compilation terminated.
when I run as
我已经包含 using namespace std;
并将字符串包含为#include <string>
. 在每个头文件中,无论我在哪里使用 string.h。但即使我在fatal error: string: No such file or directory compilation terminated.
跑步时得到
g++ ExampleProgram.cpp SudokuSolver.h Cell.h Error.h Pos.h Matrix.h
g++ ExampleProgram.cpp SudokuSolver.h Cell.h Error.h Pos.h Matrix.h
When I compile the ExampleProgram.cppas
当我将 ExampleProgram.cpp 编译为
g++ -c ExampleProgram.cpp SudokuSolver.h
Cell.h Error.h Pos.h Matrix.h
I am getting no error.
我没有收到任何错误。
When I run the ExampleProgram.cppusing ./a.out
I am not getting the output for my sudoku solver. Rather I am getting the output of my previously runned helloworld program. It shows my ExampleProgram.cpp
has not successfully compiled. But as previously said
当我使用ExampleProgram.cpp 运行时,./a.out
我没有获得数独求解器的输出。相反,我正在获取我之前运行的 helloworld 程序的输出。它显示我的ExampleProgram.cpp
没有成功编译。但正如之前所说
g++ -c ExampleProgram.cpp SudokuSolver.h
Cell.h Error.h Pos.h Matrix.h
doesn't give any error.
没有给出任何错误。
This is my output:
这是我的输出:
[fosslab@fosslab SudokuSolver]$ g++ -c Error.h
[fosslab@fosslab SudokuSolver]$ g++ -c Exception.h
[fosslab@fosslab SudokuSolver]$ g++ -c Matrix.h
[fosslab@fosslab SudokuSolver]$ g++ -c Cell.h
[fosslab@fosslab SudokuSolver]$ g++ -c Pos.h
[fosslab@fosslab SudokuSolver]$ g++ -c SudokuSolver.h
[fosslab@fosslab SudokuSolver]$ g++ -c ExampleProgram.cpp SudokuSolver.h Excepti
on.h Cell.h Error.h Pos.h Matrix.h
[fosslab@fosslab SudokuSolver]$ ./a.out
hello world[fosslab@fosslab SudokuSolver]$
回答by Some programmer dude
First of all, header files a meant to be includedin the source files. Do not add them to the command line for the compiler.
首先,头文件 a 意味着要包含在源文件中。不要将它们添加到编译器的命令行中。
Secondly, the command line argument "-c" to g++ tells g++ to not link and make an executable file, but only make an object file.
其次,g++ 的命令行参数“-c”告诉 g++ 不链接和生成可执行文件,而只生成目标文件。
You should either do:
你应该这样做:
$ g++ -c ExampleProgram.cpp
$ g++ ExampleProgram.o
or
或者
$ g++ ExampleProgram.cpp
In both the above cases you will get a new "a.out" executable program.
在上述两种情况下,您都将获得一个新的“a.out”可执行程序。
If you have several sourcefiles, then compile them and not the header files:
如果您有多个源文件,则编译它们而不是头文件:
$ g++ -c Error.cpp
$ g++ -c Exception.cpp
$ # etc...
$ g++ -c ExampleProgram.cpp
$ g++ Error.o Exception.o ... ExampleProgram.o
回答by Jayalatha Gali
I know its late but my answer might help others who are facing this issue. It is quite possible that you don't have libstdc++-devel package on your system. Please recheck. I faced the same issue and could resolve it by installing the devel package.
我知道为时已晚,但我的回答可能会帮助面临此问题的其他人。您的系统上很可能没有 libstdc++-devel 包。请重新检查。我遇到了同样的问题,可以通过安装开发包来解决它。