Write a macro for C / C ++ #include

I am working on AS / 400, which is sometimes not POSIX. We also need to compile our code on UNIX. We have a problem with something simple: #include.

In AS / 400, we need to write:

  #include "* LIBL / H / MYLIB" 

On UNIX, we need to write

  #include "MYLIB.H" 

Right now, we have this (ugly) block at the top of every C / C ++ file:

  #ifndef IS_AS400
     #include "* LIBL / H / MYLIB"
     / * others here * /
 #else
     #include "MYLIB.H"
     / * others here * /
 #endif

We need a unified macro. Is it possible? I don’t know how to write it down.

Ideally, the resulting syntax would be as follows:

  SAFE_INCLUDE ("MYLIB") 
which will expand correctly on every platform.

Please inform.

+4
source share
3 answers

You can simply #include to create a separate header in each of the source files containing this ugly #ifndef once. In general, this is a common practice.

+6
source

You can define prefixes for your platform as a macro. how

 #define STRINGY(STR) #STR #define SAFE_INCLUDE(HDR) STRINGY(HDR) #ifndef IS_AS400 #define SAFE_INCLUDE(LIB) STRINGY(*LIBL/H/##LIB) #else #define SAFE_INCLUDE(LIB) STRINGY(LIB##.H) #endif 

and you can use this as

 #include SAFE_INCLUDE(MYLIB) 
+5
source

There are two best solutions:

  • Use your Makefiles to properly set the path the compiler is looking for. For GCC, you add to CFLAGS -I <path> (you can do this several times).
  • Wrap incompatible libraries with your own header files.
+4
source

All Articles