Dumbo
Dumbo

Reputation: 14102

How to tell if program is running on x86/x64 or ARM Linux platforms

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?

Upvotes: 5

Views: 8182

Answers (1)

Rolbrok
Rolbrok

Reputation: 318

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    

Upvotes: 7

Related Questions