简体   繁体   English

如何通过C ++预处理器#include包含文件的一部分

[英]How to include part of file by C++ preprocessor #include

I want to refer some stable library code which is not maintained by me. 我想引用一些我不维护的稳定库代码。 Actually it is some MFC code snippet. 实际上,这是一些MFC代码段。

But, whenever I want to include the code snippet, I have to #include entire file, which consequently I have to include other stuff, then the whole MFC ... The consequence is not acceptable. 但是,每当我想包含代码片段时,都必须#include整个文件,因此必须包含其他内容,然后是整个MFC ...结果是不可接受的。

Currently, I copy/paste the code snippet into my project, but I feel disgraceful. 目前,我将代码段复制/粘贴到我的项目中,但是我感到可耻。 Can I just refer part of a file by C++ preprocessor? 我可以只使用C ++预处理程序来引用文件的一部分吗?

Even the code is hard-linked with specific MFC version, it is better than duplicate them in my project. 即使代码与特定的MFC版本硬链接,也比在我的项目中复制它们更好。 With such hard-link, I will know it's from MFC and save my time to check them. 有了这样的硬链接,我将知道它来自MFC,并节省了检查时间。

Is there some super #include usage? 有一些超级#include用法吗?


Can we write something like 我们可以写点什么吗

#include  "foo.h" line [12, 55)

which means to include line 22 to 54 for foo.h 这意味着在foo.h中包含第22至54行

What some have done is write #ifdef-sections in their headers to allow including files to only get specific parts. 有些人所做的是在其标头中写入#ifdef-sections,以允许包括文件的内容仅包含特定部分。 I don't know if your MFC file has those but you can look through it and use any existing ones or write your own. 我不知道您的MFC文件是否包含这些文件,但是您可以浏览并使用任何现有文件或编写自己的文件。

The header usually look something like this 标头通常看起来像这样

#ifdef USE_FANCYPANTS
bool hasFancyPants();
#endif

#ifdef USE_COOLSTUFF
void doCoolStuff();
#endif

And your include files then use #define before including. 然后您的包含文件在包含之前使用#define。

#define USE_FANCYPANTS
#include "header.hpp"

Then you only get hasFancyPants() and not doCoolStuff() 那么你只会得到hasFancyPants()而不是doCoolStuff()

  • You can use conditional compilation to inlcude/exclude stuff you dont need. 您可以使用条件编译来包含/排除不需要的内容。 You have to change the source code slighty, and amend project settings. 您必须稍微更改代码,并修改项目设置。
  • Can use typedef keyword, which would define types differently for MFC and non-MFC, and/or specific to your project settings, and the legacy code. 可以使用typedef关键字,它将为MFC和非MFC定义不同的类型,和/或特定于您的项目设置和旧版代码。
  • You may put entire stuff in a DLL or a .LIB (having code, not just declaration), and put the linker pragma in header file itself. 您可以将全部内容放入DLL或.LIB(具有代码,而不仅是声明),并将链接器编译指示放在标头文件本身中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM