Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you use #define to change a #include?

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?

like image 495
Tormyst Avatar asked Jul 08 '14 14:07

Tormyst


People also ask

Can I use VH VW?

vh and vw in actionTo use vh and vw values, just type “Nvh” or “Nvw” (where “N” represents the percentage of the viewport you'd like to cover) into any width or height field. So to cover 100% of the viewport, you'd set 100% for the width and 100vh for the height.

Can I use max width fit content?

fit-content as min- or max-widthYou can also use fit-content as a min-width or max-width value; see the example above. The first means that the width of the box varies between min-content and auto, while the second means it varies between 0 and max-content.


1 Answers

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.

like image 151
zwol Avatar answered Nov 14 '22 22:11

zwol