如何判断程序是否在x86/x64或ARM Linux平台上运行 [英] How to tell if program is running on x86/x64 or ARM Linux platforms

查看:1772
本文介绍了如何判断程序是否在x86/x64或ARM Linux平台上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

c程序中,我想做不同的事情.该程序将在基于x86/x64的GNU/Linux系统以及基于ARM的系统上运行,例如在PC或RaspberryPI上.

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.

GCC中是否有预定义的宏可以告诉平台?

Is there predefined macros in GCC to tell the platform?

类似

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

也许这是错误的方法?我将使用Makefile进行编译,并且可以摆脱自己的定义.

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?

推荐答案

这些帖子已经回答了:
用于架构X的GCC预定义宏

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

您可以在这里拥有它们:
http://sourceforge.net/p/predef/wiki/Architectures/

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.

修改:
基本上,因为链接可能变得无效:
__arm__应该可以在ARM上使用.
__x86_64__应该适用于x64体系结构.


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

是的,您可以这样做:

#ifdef __x86_64__    
    // do x64 stuff   
#elif __arm__    
    // do arm stuff
#endif    

这篇关于如何判断程序是否在x86/x64或ARM Linux平台上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆