0

In a C project, at some places, I'd like to compile different code, depending on whether or not the compilation happens for x64. Of course, CPP macros are the route to go.

However, on this page (which should be the authoritative reference), there is no predefined macro just for x64.

Out of curiosity, did I miss something?

I am aware that I easily can solve the actual problem:

First, according to the reference above, if I would like to stick with predefined macros, I could do something like that:

#if defined (_M_AMD64) && (_M_AMD64 == 100) && !defined (_M_ARM64EC)
....
#endif

The first two conditions make sure we compile for x64 or arm64ec, the third condition makes sure we do not compile for arm64ec, which together should make sure we compile for x64.

Second, I could define my own macro in the project settings for x64 configurations and use that instead of a predefined one:

#ifdef MY_OWN_X64_FLAG
...
#endif

While they solve the problem, both methods are not intuitive and elegant. Is there really no predefined macro that we can use to test for x64 only with one simple #ifdef?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Binarus
  • 4,005
  • 3
  • 25
  • 41
  • The "obvious" `_M_X64` suffers from the same braindamage as `_M_AMD64`. You have to wonder what went wrong. It appears the ARM64 code was copied straight from the AMD64 code, and that explains why there is no answer. – MSalters Sep 27 '22 at 15:16
  • there's no simpler ways than to use [boost platform predefs](https://stackoverflow.com/a/52992284/995714) – phuclv Feb 12 '23 at 10:00
  • [*"When you compile your code as ARM64EC, the architecture preprocessor symbols will say that you are compiling for x86-64. There’s a reason for this. See the linked article for the answer"*](https://devblogs.microsoft.com/oldnewthing/20220830-00/?p=107069) – phuclv Feb 12 '23 at 10:26

1 Answers1

0

did I miss something?

No.

Is there really no predefined macro that we can use to test for x64 only with one simple #ifdef?

Yes, according to your own research and to the linked documentation, there is really no predefined macro to test for x86_64 with one simple #ifdef on that compiler.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111