views:

44

answers:

3

I'm trying to simplify the deployment of an application. In order to build the final application on an end-user's machine, a couple of C files need to be compiled. This means that dozens of header files need to be shipped along with the application. I'd like to be able to pre-include the contents of the include files, but I also need to be able to control the directives (#if, etc.) after the includes are in-lined. I can't find a cpp option that lets me just include headers, without doing the rest of the preprocessing. What are my options?

Example:

File1.h

void dummy_func()
{return;}

File2.h

#if INCLUDE_FILE1
    #include "file1.h"
#endif

In the end, I want a file that says:

#if INCLUDE_FILE1
void dummy_func()
{return;}
#endif
A: 

What about using an #ifdef to control which #if sections are compiled?

John at CashCommons
Unfortunately, I've got a LOT of files in an existing toolkit that already have these things defined. Manually editing them isn't a great option. Or do you have something simpler in mind?
Wandercoder
I'm not sure what I had in mind. :) But Nicolas is more on track for what you want. Depending on your IDE there should be some installation package facilities available that you can use.
John at CashCommons
A: 

In order to build the final application on an end-user's machine, a couple of C files need to be compiled. This means that dozens of header files need to be shipped along with the application.

So what? If you put all the files (including headers) into a tarball, and you have a sane build system (you do have at least a Makefile, don't you?), what's the problem with having a lot of files?

Nicolás
Actually, no, I don't have a Makefile. It's a Windows-based application. Yes, one possibility is shipping a boat-load of extra files along, bloating the deployment and making it more stuff to remember to include in the deployment. Of course, one answer is "don't do what you're trying to do." I may be insane for thinking of doing it, but I'm asking you to feed my insanity, not talk me out of it. At least not yet. :-)
Wandercoder
Hmm let's put it this way. Your "problem" is far from unique: How many people have multi-file C++ Windows applications they want to deploy easily? How many of those do you think would try to concatenate all their source files into one? :P There *has* to be a better way (= a sane build script).
Nicolás
A: 

Due to double-include guards, a tool that inlines #includes may cause a giant file, where a lot of the headers are entirely inside #ifndefs that don't match. In extreme cases, it may even cause an infinite-size output file, if includes are recursive (which normally isn't a problem because of the double-include guards).

I just confirmed that this code compiles successfully:

main.cpp:

#include "a.h"
int main() { return 0; }

a.h:

#ifndef A_H_INCLUDED
#define A_H_INCLUDED

#include "b.h"

#endif

b.h:

#ifndef B_H_INCLUDED
#define B_H_INCLUDED

#include "a.h"

#endif

Since you want to keep ifdefs untouched and unparsed, a tool that inlines #includes would cause infinite output.

Nicolás
Ouch! That's a good rationale for why it wasn't a cpp option. I had a feeling that there was a reason and someone would tell me why. You may have talked me out of my insanity after all. ;-) I'll give it some more thought. Thanks, Nicolas.
Wandercoder