2

I want to refer some stable library code which is not maintained by me. Actually it is some MFC code snippet.

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.

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?

Even the code is hard-linked with specific MFC version, it is better than duplicate them in my project. With such hard-link, I will know it's from MFC and save my time to check them.

Is there some super #include usage?


Can we write something like

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

which means to include line 22 to 54 for foo.h

4
  • 1
    Yes - 1) you can "block out" the parts of the header you don't want with "#ifdef/#ifndef/#endif". Of course that means modifying the header. 2) Alternatively, you can cut/paste the parts you want into a new header. Commented Jan 22, 2013 at 7:26
  • 3
    It will probably work out cleanest, IMO, if you create a header of your own and copy the snippet into that, once per version of MFC, and then use your header. Not particularly nice, but better than hacking a system-supplied header (that is very much a last resort). Commented Jan 22, 2013 at 7:39
  • You are talking about to include header stuff, aren't you? Or should it be you are trying to include any sorts of implementing code? For the latter: Do not do that, but build an object to link to. Commented Jan 22, 2013 at 7:47
  • Fiddling with compiler headers is a bad idea for many reasons. But apart from that, it doesn't seem like a particularly bright idea to refer to a header file for the sake that it is maintained by someone else, then make sure that you have to manually edit said header file for it to work. Commented Jan 22, 2013 at 7:48

2 Answers 2

7

What some have done is write #ifdef-sections in their headers to allow including files to only get specific parts. 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.

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 USE_FANCYPANTS
#include "header.hpp"

Then you only get hasFancyPants() and not doCoolStuff()

Sign up to request clarification or add additional context in comments.

Comments

0
  • 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.
  • 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.