4

In a c program I want to do different things. This program will run on x86/x64 based GNU/Linux system as well as ARM based one e.g. on a PC or RaspberryPI.

Is there predefined macros in GCC to tell the platform?

something like

#ifdef _X64_
   /do x64 stuff
#elif _ARM_
//do arm stuff
#endif

Or maybe that is the wrong approach? I will be using Makefileto compile and I could get away with my own defines.

What would be the best/safest approach?

Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • 1
    not "run" but "compiled for". Yes, this is right approach. – Eugene Sh. Jan 12 '16 at 15:44
  • 1
    Possible duplicate of http://stackoverflow.com/questions/15860850/gcc-predefined-macros-for-architecture-x – mikedu95 Jan 12 '16 at 15:44
  • Possible duplicate of [Detecting CPU architecture compile-time](https://stackoverflow.com/questions/152016/detecting-cpu-architecture-compile-time) – phuclv Sep 15 '18 at 11:32

1 Answers1

6

This has already been answered on these posts:
GCC predefined macros for architecture X, Detecting CPU architecture compile-time

You can have them here:
http://sourceforge.net/p/predef/wiki/Architectures/

Your approach should only be used for small portions of code or functions but it should work.

Edit:
Basically, because links can become invalid:
__arm__ should work on ARM.
__x86_64__ should work on x64 architecture.

And yes, you can do:

#ifdef __x86_64__    
    // do x64 stuff   
#elif __arm__    
    // do arm stuff
#endif    
Community
  • 1
  • 1
Rolbrok
  • 308
  • 1
  • 7
  • This answers the question sufficiently. Inlining content from the linked page only makes it more likely the answer will be obsolete some day. – Jeff Hammond Jan 13 '16 at 02:46