Just compile it, [check output], and run it using 'system()'.
[ untested. just for an idea. ]
std::string comp( "g++ " );
std::string cpplibs( "-lmylib " );
std::string cppfile( "MyFile.cpp " );
std::string Command = "\"" + comp + cppfile + cpplibs + "\"";
system( Command.c_str() );
// check it here
system( MyFile );
Get the source for some IDE, and examine how they do it.
Can you explain that a bit more? It might be just what I need.
Just to clear up what I need it for:
I need it the main program to dynamically (At runtime) genarate code.
The only way I can see for this to be done is to (have the program)
write it to a file then load it back at a later time.
Always attack the problem in small steps. First write a program that writes
a simple program.
// - includes here -
#include <iostream>
#include <fstream>
int main(){
std::cout<<"Hello world!"<<std::endl;
std:
fstream out( "mymain.cpp" );
if( not out.is_open() ){
return EXIT_FAILURE;
} // if()
out<<"\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"<<std::endl;
return 0;
} // main()
Compile and run that. Then, with your compiler, compile and run "mymain.cpp"
to test it.
Then you can add in (just before 'return 0;' in main() ) that earlier code
(above), modify the strings to fit your compiler, and try it. (no
guarantees!! <G>).
Unfortunately, what goes into the paranthesis in a 'system()' call is not
really on topic in this NG. Other than that, post back if you have trouble.
I need to be able to create and run a large number of files without
having to code in a loading mechanism for each one.
You might do something like:
// .....
std::vector<std::string> programs;
std::string pgm1( "\n\n// - includes here -\n"
"#include <iostream>\n\n"
"int main(){\n"
" std::cout<<\"Hello world!\"<<std::endl;\n"
" return 0;\n"
" } // main()\n"
);
programs.push_back( pgm1 );
// .....
// ofstream out( .... );
out<<programs.at( 0 );
// .....