Can I expand #include files inline and not expand directives?
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 ju开发者_StackOverflow社区st 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
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.
What about using an #ifdef to control which #if sections are compiled?
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?
精彩评论