0

Following these related questions (1,2,3), I'm trying to get OS information from the preprocessor as follows:

#include <boost/preprocessor/stringize.hpp>
#ifdef __MACH__
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )
#endif

clang 13.1.6 prints:

warning: MACH: 1 [-W#pragma-messages]
#pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )

and gcc 11.3.0 :

note: '#pragma message: MACH: 1'
4 | #pragma message ("MACH: " BOOST_PP_STRINGIZE(__MACH__) )

This tells me that the macro __MACH__ is defined, but I don't get details about the OS version.

I want to print the version of the operating system or print all the macros that are defined using the preprocessor.

ilciavo
  • 3,069
  • 7
  • 26
  • 40
  • not quite sure what you're looking for: Pragmas are dealt with at *compile* time, but the preprocessor/compiler can't know which operating system your program will be executed on; and the OS the program was compiled on should have exactly zero influence on the program. Can you say *exactly* why you want things to be printed? What is it that you want to find out? – Marcus Müller May 29 '22 at 14:46
  • 1
    In other words: `This tells me that I'm working with OSX, but I don't get details about the version.` No, it tells you you're compiling code where the `__MACH__` preprocessor macro is defined, which is usually the case when your compiler is targetting a MACH architecture (but not, say, a *particular* version of the OS X operating system). It does **not** say that you're currently working on Mach! You might very well be cross-compiling, for example. You're not *getting info from the operating system*, you're getting info from the preprocessor. – Marcus Müller May 29 '22 at 14:50
  • macOS Sierra 10.12 and later support `clock_gettime`. I want to tell the compiler which headers to use depending on the version of the OS. This is the reason I want to know which macros are defined, including `__MACH__` and `__APPLE__`. – ilciavo May 29 '22 at 14:54
  • but at compile time, you **Cannot** know on which version of OS X you'll run. So, this is logically impossible, not even a problem on a preprocessor/pragma/c++ level. You need to draw a stricter distinction between "build time" and "run time" in your head, here! – Marcus Müller May 29 '22 at 15:12
  • It’s my understanding, that I need an OS, a working compiler, and a set of libraries to build and another OS to run. For my specific case, these two systems happen to be the same. The information I need is from the building system, such that the compiler can know which headers are available. If this is impossible, that’s it – ilciavo May 29 '22 at 15:50

1 Answers1

1

I'm trying to get OS information from the preprocessor

Your preprocessor runs before your code is even compiled.

The execution on an OS of a specific version happens at a later time, not necessarily (or even usually) on the same machine.

Hence, for reasons of this universe being bound by causality, your preprocessor can't know the version of the OS your software is run on.

Thanks to the clarifying edit, I understand you want to figure out whether the development libraries that you're currently using contain a specific function. But that has nothing to do with detecting your OS! You could be using more modern headers on an old OS, and vice versa.

What you're looking for is usually called a feature test macro.

In your specific case, the feauture you need to check for should be POSIX.1 compatibility (#if _POSIX_C_SOURCE >= 199309L, see the bottom of the OS X man page), but I don't know whether OS X implemented that correctly.

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94