4

I work on AS/400 which is sometimes non-POSIX. We also need to compile our code on UNIX. We have an issue with something as simple as #include.

On AS/400, we need to write:

#include "*LIBL/H/MYLIB"

On UNIX, we need to write

#include "MYLIB.H"

At the moment we have this (ugly) block at the top of each C/C++ file:

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

We would like a unified macro. Is this possible? I don't know how to write it.

Ideally, the resulting syntax would be:

SAFE_INCLUDE("MYLIB")
that would expand correctly on each platform.

Please advise.

m7913d
  • 10,244
  • 7
  • 28
  • 56
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • The form I've used for at least the last ten years is `#include `. – user2338816 Apr 04 '14 at 11:43
  • That's kind of an interesting requirement. Does AS400 *not* allow a dot in filenames? – jww Oct 05 '15 at 20:36
  • @jww: AS400 has fundamentally different file system than Unix/Linux/BSD/Windows. First, the content is not "free form"; second, the hierarchy is severly limited. See also: VMS file system. – kevinarpe Oct 08 '15 at 01:43
  • OK, thanks. I occasionally worked with an AS400 years ago, but it provided SAMBA, so I did not suffer those limitations. The trouble I had was I could not get filesystem change notifications because it was not a native Windows machine or a Windows filesystem. I think I was using [FindFirstChangeNotification](https://msdn.microsoft.com/en-us/library/windows/desktop/aa364417%28v=vs.85%29.aspx). – jww Oct 08 '15 at 02:49

3 Answers3

6

You can simply #include some separate header in every of your source files containing that ugly #ifndef just once. It's a common practice anyway.

unkulunkulu
  • 11,576
  • 2
  • 31
  • 49
5

You can define prefixes for your platform as macro. Like

#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)
useraged
  • 1,706
  • 17
  • 34
  • Yeah, that's more versatile than my advise in the case where there are more than one such header. Should be accepted if it's the case. – unkulunkulu Jul 25 '11 at 08:32
4

There are two better solutions:

  1. Use your Makefiles to properly set a path where compiler looks for includes. For GCC you add to CFLAGS -I <path> (you can do that multiple times).
  2. Wrap the non-compliant libraries with your own header files.
Rafał Rawicki
  • 22,324
  • 5
  • 59
  • 79
  • 1
    The problem is when header files are not "header.h" but "h/header" or "h(header)". All file systems are not created equal! – Bo Persson Jul 25 '11 at 16:34