• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C++ combine all files into single .cpp with g++

Red Squirrel

No Lifer
Is there a way with G++ to compile, but have it generate a .cpp file that has all the contents of all the includes (in order) but in one file? That includes the stuff like iostream etc....

Basically what I want to be able to create is a stand alone .cpp file that I can put on another PC for quickly recompiling a program without worrying about header dependencies/paths etc.

Of course I'd still need to have any libraries installed and what not.
 
Originally posted by: RedSquirrel
Is there a way with G++ to compile, but have it generate a .cpp file that has all the contents of all the includes (in order) but in one file? That includes the stuff like iostream etc....

Basically what I want to be able to create is a stand alone .cpp file that I can put on another PC for quickly recompiling a program without worrying about header dependencies/paths etc.

Of course I'd still need to have any libraries installed and what not.

Wha? g++ doesn't generate cpp files, you do. cpp files generally contain your source code.

If you want an object file, then yes, it is possible with g++ -c filename.cpp that will create filename.o which has all the header info.

The problem you'll run into is that iostream uses templates. Templates are figured out at compile time, so you won't be able to use them really.

If the headers are part of standard c++ (IE iostream) then go ahead and just distribute the source, and compile on the spot.
 
You might be able to use the C Preprocessor, cpp. I'm not sure if it handles C++. I would be surprised if what you're trying to do ends up working.
edit:
Wow, the "attach code" feature really sucks.

$ cat foo.c
#include "foo.h"

int main()
{
return doit();
}

$ cat foo.h
void doit() {
return 0;
}

$ cpp foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "foo.c"
# 1 "foo.h" 1
void doit() {
return 0;
}
# 2 "foo.c" 2

int main()
{
return doit();
}
 
This may be a worthless comment, but in Visual C++ the /P compiler switch will preprocess to a file, which seems exactly what you want. I don't use GCC regularly so I don't know if it has an equivalent switch, but I guess it's a starting point.
 
Back
Top