Question

My company has 1000s of source files that use a macro to specify the include path. I cannot change that and it works with the current compilers. Now, I try to compile that with GCC (4.8.2 or 3.4.6) and it fails.

Basically there is a macro like that:

#define COMP_INC(comp, file) <comp/include/file>

That is used like that:

#include COMP_INC(posix, inttypes.h)

My problem is that GCC is for some unknown reason translating my macro call into that:

#include <posix/include/ inttypes.h>

Notice the space before "inttypes.h"? Because of that, the header file is not found.

Why does the preprocessor insert a space? Can I avoid it? I can only change the macro, I cannot change the source files that uses it.

Was it helpful?

Solution

Remove the space between posix and inttypes.h (after the comma). The problem goes away.

This answer tells us why this occurs:

From the C99 standard:

A preprocessing directive of the form

# define identifier replacement-list new-line

defines an object-like macro that causes each subsequent instance of the macro name to be replaced by the replacement list of preprocessing tokens that constitute the remainder of the directive.


So macros work on tokens and the whitepace is to be expected.

EDIT:

According to this answer, you can use the traditional-cpp flag to mimic the old behavior of not adding the space. I am not sure how that will affect your build, but you can try it:

Only much older preprocessors didn't insert that extra space -- note that original entry was submitted over 20 years ago in 1988, before the 1989 version of the C standard was standardized. You can pass the -traditional-cpp flag to the GCC preprocessor, which causes it to imitate the behavior of old-fashioned C preprocessors, as opposed to ISO C preprocessors.

OTHER TIPS

If you can accept to predefine all possible 'comp'-names and 'file'-names:

#define MAKE_INCLUDE(comp, file) <comp/include/file>

#define NAME_posix posix
#define NAME_inttypes inttypes

#define MAKE_NAME(x) NAME_##x

#define COMP_INC(comp, file) MAKE_INCLUDE(MAKE_NAME(comp), MAKE_NAME(file))

#include COMP_INC(posix, inttypes.h)

Note: A suffix like '.h' can not be part of a macro. Hence, directories like lib-1.0-A and lib-1.0-B will be indistinguishable (but it might be no issue, here).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top