如何使用预处理器根据平台或架构有条件地编译代码?

问题描述 投票:0回答:1

我正在编写需要在多个平台或架构上运行的代码,例如 x86、ARM 或 MIPS。 我想限制或有时必须为特定架构编译代码的不同部分。

我尝试使用

#ifdef
为特定架构编写代码。

#ifdef ARM_TYPE
// Arm-specific code goes here
#else
// Unix/Linux-specific code goes here
#endif

虽然它使得代码要重写。 想知道这是否可以更好地处理,是否有任何其他方法可以使它变得更好?

c linux conditional-compilation
1个回答
1
投票

使用 GCC,您可以使用

#ifdef __arm__
。其他编译器有其他预定义的宏。有关更多信息,请参见https://sourceforge.net/p/predef/wiki/Home

© www.soinside.com 2019 - 2024. All rights reserved.