The following is my C++ program:
main.cpp
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
return 0;
}
When the above program is executed, it creates a text-file named "firstFile.cpp" containing the following code:
firstFile.cpp
#include <iostream>
int main() {
std::cout << "hello world" << std::endl;
return 0;
}
which, when executed, prints "hello world" on the screen.
So, I would like to add to the main.cpp file lines of code asking GCC to compile the new firstFile.cpp just created.
I am using GNU gcc on both platform Ubuntu and Windows.
Is it possible to get any error code form the call to the compiler? If not why.
g++ also has additional macros. So you can compile C code with g++ and in fact mix both C and C++ code. Finally, the executable size may also changes based on the compiler.
Type gcc c –o [program_name].exe [program_name]. c and press ↵ Enter . Replace "[program_name]" with the name of your source code and application. Once the program is compiled, you'll return to the command prompt without errors.
Usually, a first compiler is written in another language (directly in PDP11 assembler in this case, or in C for most of the "modern" languages). Then, this first compiler is used to program a compiler written in the language itself. You can read this page about the history of the C language.
This is not too difficult using the std::system command. Also raw string literals allow us to insert multiline text which is useful for typing in program parts:
#include <cstdlib>
#include <fstream>
// Use raw string literal for easy coding
auto prog = R"~(
#include <iostream>
int main()
{
std::cout << "Hello World!" << '\n';
}
)~"; // raw string literal stops here
int main()
{
// save program to disk
std::ofstream("prog.cpp") << prog;
std::system("g++ -o prog prog.cpp"); // compile
std::system("./prog"); // run
}
Output:
Hello World!
gcc
is an executable, so you have to use either system("gcc myfile.cpp")
or popen("gcc myfile.cpp")
, which gives you a filestream as result.
But since you are generating code anyways, you don't even need to write it to a file. You can open the gcc proces with FILE* f = popen("gcc -x ++ <whatever flags>")
. Then you have you can write your code with fwrite(f, "<c++ code>")
. I know this is c
and not really c++
but it might be useful. ( I don't think there is a c++
version of popen()
).
All you need to do is add the following line after you create your file.
system("g++ firstFile.cpp -o hello");
Works on OS X so I hope it will work for you too.
To use the command line of a compiler in source file use system function.
Syntax of which is :
int system (const char* command); //built in function of g++ compiler.
In your case, it should be like
system("g++ firstFile.cpp");
PS: system function does not throw Exceptions.
Program
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main() {
ofstream fileWriter;
fileWriter.open ("firstFile.cpp");
fileWriter << "#include <iostream>" << endl;
fileWriter << "int main() {" << endl;
fileWriter << "\tstd::cout << \"hello world\" << std::endl;" << endl;
fileWriter << "\treturn 0;" << endl;
fileWriter << "}" << endl;
fileWriter.close();
system("g++ firstFile.cpp");
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With