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,
#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.cppuses these header files, to solve a Sudoku.
(All the .h and .cpp files are in a same folder.)
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
g++ ExampleProgram.cpp SudokuSolver.h Cell.h Error.h Pos.h Matrix.h
When I compile the ExampleProgram.cpp as
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
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]$
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.
Secondly, the command line argument “-c” to g++ tells g++ to not link and make an executable file, but only make an object file.
You should either do:
or
In both the above cases you will get a new “a.out” executable program.
If you have several source files, then compile them and not the header files: