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.
在c程序中,我想做不同的事情。該程序將在基於x86 / x64的GNU / Linux系統以及基於ARM的系統上運行。在PC或RaspberryPI上。
Is there predefined macros in GCC to tell the platform?
GCC中是否有預定義的宏來告訴平台?
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.
或者這可能是錯誤的做法?我將使用Makefileto編譯,我可以使用自己的定義。
What would be the best/safest approach?
什么是最好/最安全的方法?
1 个解决方案
#1
2
這些帖子已經得到了回答:GCC預定義的架構X宏,檢測CPU架構編譯時
你可以在這里找到它們: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.
編輯:基本上,因為鏈接可能變得無效:__ arm__應該適用於ARM。 __x86_64__應該適用於x64架構。
And yes, you can do:
是的,你可以這樣做:
#ifdef __x86_64__
// do x64 stuff
#elif __arm__
// do arm stuff
#endif