Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ask, programmatically, a compiler to compile a file in C++?

Tags:

c++

gcc

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.

like image 896
Ninda Avatar asked Mar 22 '16 09:03

Ninda


People also ask

Can you use g ++ to compile C?

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.

Which command is compile C using gcc 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.

How is C compiler compiled?

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.


4 Answers

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!
like image 63
Galik Avatar answered Oct 07 '22 05:10

Galik


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()).

like image 45
David van rijn Avatar answered Oct 07 '22 05:10

David van rijn


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.

like image 13
Artur Nowicki Avatar answered Oct 07 '22 05:10

Artur Nowicki


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;
}
like image 7
secretgenes Avatar answered Oct 07 '22 03:10

secretgenes