linux x64 运行x86程序,如何判斷程序是在x86 / x64還是ARM Linux平台上運行

在C程序中,需要为x86/x64和ARM架构实现不同功能。GCC提供了预定义的宏来识别平台。可以使用`__arm__`和`__x86_64__`宏来区分代码,例如`#ifdef __x86_64__`和`#elif __arm__`。这种方法适用于代码的特定部分,通过Makefile进行编译时,也可以自定义宏来实现平台区分。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值