6

Is it possible to change the library included using #include <foo> or #include "foo" to something different during prepossessing so it would instead act as a different library, for example #include <bar>? I have a library that is not working with the current #include statement in just one context, but works fine elsewhere, so I don't want to change it directly. Would it be possible to use #define to fix this?

3
  • #ifdef foo #define bar #ifndef foo #define foobar #endif
    – cerkiewny
    Commented Jul 8, 2014 at 14:55
  • 9
    yes, you could do a #ifdef SOMEVAR and then #include <foo> #else #include <bar>
    – EdChum
    Commented Jul 8, 2014 at 14:56
  • You can also do #define FILENAME "something" (where the definition is based on other macros) followed by #include FILENAME to include a different file. Commented Jul 8, 2014 at 15:00

2 Answers 2

9

There are two ways to do this. The simpler, more obvious way:

#define INCLUDE_FOO

// ...

#ifdef INCLUDE_FOO
#include <foo>
#else
#include <bar>
#endif

And the shorter, but finickier way:

#define FOO_HEADER "foo"

// ...

#include FOO_HEADER

You have to be careful if you use the second way, because the C standard does not fully define the behavior of #include followed by anything which is not either "..." or <...>. It says that the "anything which is not..." is fully macro-expanded, not applying the special tokenization rules for #include lines (e.g. <foo.h> is five tokens, not one) and then something implementation-defined happens.

If the result of full macro expansion is a single string literal token, all implementations I know about will do what you expect, i.e. they will treat that as if #include "..." had been written, where the ... is the contents of the string literal. (However, the behavior of backslashes within the string literal may be not as you expect. Use only forward slashes for directory separators; that works reliably on Windows as well as elsewhere.)

If the result of full macro expansion is anything else, the behavior is unpredictable and differs not only between implementations, but between point releases of the same implementation. Avoid.

Addendum: If an #include line is written in one of the two typical formats to begin with...

#include "foo"
#include <foo>

... then macro expansion does not happen and cannot be forced to happen. This means you are probably up a creek wrt your desire to avoid changing the third-party header with the problem.

1
  • This is an interesting way of doing things. I guess you could add some #ifndef logic to make it slightly better so at least something is defined for the #include. Thanks for the idea.
    – Tormyst
    Commented Jul 8, 2014 at 15:42
4

Basically in your cpp file you can define a variable that toggles the behaviour of the include file:

So in a.cpp

#define BAR
#include "myHeader.h"

in "myheader.h:"

#ifdef BAR
#include <bar>
#else
#include <foo>
#endif

There is a good GOTW article on other ways you can use preprocessor macros to toggle behaviour

3
  • That works. Thank you. Just wondering, is there a way to do this without having to change the library at all?
    – Tormyst
    Commented Jul 8, 2014 at 15:04
  • I don't think so, I'm just racking my brains to see if you #undef it I think not because you are controlling the behaviour of the header file so the file has to know how to toggle the header files it needs to include
    – EdChum
    Commented Jul 8, 2014 at 15:09
  • I was thinking if you could #undef #include but that is not allowed and even if it was it doesn't change what it includes, so I think you can't do this without modifying the header
    – EdChum
    Commented Jul 8, 2014 at 15:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.