2012-02-03 676 views
0

我一直在掙扎超過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.

(所有的.h和.cpp文件處於同一個文件夾。)

我已經包括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

當我編譯ExampleProgram.cpp

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

我沒有收到任何錯誤。

當我運行ExampleProgram.cpp使用./a.out我沒有得到我的數獨求解器的輸出。相反,我得到了我以前運行的helloworld程序的輸出。它顯示我的ExampleProgram.cpp未成功編譯。但如前所述

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

不會給出任何錯誤。

這是我的輸出:

所有的
[[email protected] SudokuSolver]$ g++ -c Error.h 
[[email protected] SudokuSolver]$ g++ -c Exception.h 
[[email protected] SudokuSolver]$ g++ -c Matrix.h 
[[email protected] SudokuSolver]$ g++ -c Cell.h 
[[email protected] SudokuSolver]$ g++ -c Pos.h 
[[email protected] SudokuSolver]$ g++ -c SudokuSolver.h 
[[email protected] SudokuSolver]$ g++ -c ExampleProgram.cpp SudokuSolver.h Excepti 
on.h Cell.h Error.h Pos.h Matrix.h            
[[email protected] SudokuSolver]$ ./a.out 
hello world[[email protected] SudokuSolver]$ 
+2

您不編譯頭文件。 – trojanfoe 2012-02-03 10:22:22

+1

@trojanfoe我已經編譯過了。 – 2012-02-03 10:23:36

+0

@EAGER_STUDENT不,實際上你永遠不會編譯頭文件。當你編譯(實際上在預處理階段)一個.cpp文件時,它包含使用include語句。 – 2012-02-03 10:29:17

回答

2

首先,頭文件意味着是包含在源文件。不要將它們添加到編譯器的命令行中。

其次,g ++的命令行參數「-c」告訴g ++不鏈接並生成一個可執行文件,但只生成一個目標文件。

您應該做的:

$ g++ -c ExampleProgram.cpp 
$ g++ ExampleProgram.o 

$ g++ ExampleProgram.cpp 

在上述兩種情況下,你會得到一個新的 「的a.out」 可執行程序。

如果你有幾個文件,然後編譯它們,而不是頭文件:

$ g++ -c Error.cpp 
$ g++ -c Exception.cpp 
$ # etc... 
$ g++ -c ExampleProgram.cpp 
$ g++ Error.o Exception.o ... ExampleProgram.o 
1

我知道它的晚,但我的回答可以幫助其他人誰正面臨着這個問題。您的系統中很可能沒有libstdC++ - devel軟件包。 請重新檢查。我面臨同樣的問題,可以通過安裝devel軟件包來解決它。

相關問題