我可以扩展#include文件内联而不扩展指令吗? [英] Can I expand #include files inline and not expand directives?
问题描述
我正在尝试简化应用程序的部署。为了在最终用户的机器上构建最终应用程序,需要编译几个 C 文件。这意味着需要随应用程序一起提供数十个头文件。我希望能够预先包含包含文件的内容,但我还需要能够控制指令( #if
等,包括内衬后。我找不到一个cpp选项,它允许我只包含标题,而不进行其余的预处理。我有什么选择?
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?
示例:
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
推荐答案
由于双重包含警卫,内联#includes的工具可能会导致一个巨大的文件,其中很多标题完全在#ifndefs内部,不匹配。在极端情况下,它甚至可能导致无限大小的输出文件,如果包含是递归的(由于双重包含保护通常不是问题)。
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; }
啊:
#ifndef A_H_INCLUDED
#define A_H_INCLUDED
#include "b.h"
#endif
bh:
#ifndef B_H_INCLUDED
#define B_H_INCLUDED
#include "a.h"
#endif
由于你想保持ifdefs不变和未解析,一个内联 #includes
的工具会导致无限输出。
Since you want to keep ifdefs untouched and unparsed, a tool that inlines #includes
would cause infinite output.
这篇关于我可以扩展#include文件内联而不扩展指令吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!