致命错误:字符串:没有终止这样的文件或目录编译。

时间:2021-06-10 09:39:42

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.

但是对于我的Sudoku项目,我有以下用户定义的头文件,SudokuSolver。h矩阵。h,细胞。h,Pos.h,错误。h,例外。h及其相应的。cpp文件。

And ExampleProgram.cpp uses these header files, to solve a Sudoku.

和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

我使用了名称空间std;我把字符串包括在#include 。在每个头文件中,无论我在哪里使用字符串。但是,即使我得到了致命错误:字符串:没有终止这样的文件或目录编译。当我运行,

g++ ExampleProgram.cpp SudokuSolver.h Cell.h Error.h Pos.h Matrix.h

g++ ExampleProgram。cpp SudokuSolver。细胞h。h错误。h Pos.h Matrix.h

When I compile the ExampleProgram.cpp as

当我编译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.cpp using ./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。我没有得到我的sudoku solver的输出。相反,我正在获得我之前运行的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]$ 

2 个解决方案

#1


2  

First of all, header files a meant to be included in 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.

其次,命令行参数“-c”到g++告诉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”。“可执行程序。

If you have several source files, 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

#2


1  

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包。请再核对。我遇到了同样的问题,可以通过安装devel包解决它。

#1


2  

First of all, header files a meant to be included in 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.

其次,命令行参数“-c”到g++告诉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”。“可执行程序。

If you have several source files, 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

#2


1  

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包。请再核对。我遇到了同样的问题,可以通过安装devel包解决它。