C++ 检测 CPU 架构编译时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/152016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detecting CPU architecture compile-time
提问by Alex B
What is the most reliable way to find out CPU architecture when compiling C or C++ code? As far as I can tell, different compilers have their own set of non-standard preprocessor definitions (_M_X86
in MSVS, __i386__
, __arm__
in GCC, etc).
编译 C 或 C++ 代码时找出 CPU 架构的最可靠方法是什么?据我所知,不同的编译器有自己的一套非标准的预处理器定义(_M_X86
在MSVS, __i386__
,__arm__
在海湾合作委员会等)。
Is there a standardway to detect the architecture I'm building for? If not, is there a source for a comprehensive list of such definitions for various compilers, such as a header with all the boilerplate #ifdef
s?
有没有一种标准的方法来检测我正在构建的架构?如果没有,是否有各种编译器的此类定义的完整列表的来源,例如包含所有样板#ifdef
的标头?
采纳答案by Serge
Here is some information about Pre-defined Architecture Macrosand other types of pre-defined macros.
以下是有关预定义架构宏和其他类型的预定义宏的一些信息。
This question asks where they are definedin the GCC source code.
回答by John Millikin
There's no inter-compiler standard, but each compiler tends to be quite consistent. You can build a header for yourself that's something like this:
没有编译器间标准,但每个编译器都趋于一致。您可以为自己构建一个标题,如下所示:
#if MSVC
#ifdef _M_X86
#define ARCH_X86
#endif
#endif
#if GCC
#ifdef __i386__
#define ARCH_X86
#endif
#endif
There's not much point to a comprehensive list, because there are thousands of compilers but only 3-4 in widespread use (Microsoft C++, GCC, Intel CC, maybe TenDRA?). Just decide which compilers your application will support, list their #defines, and update your header as needed.
一个完整的列表没有多大意义,因为有数千个编译器,但只有 3-4 个被广泛使用(Microsoft C++、GCC、Intel CC,也许是 TenDRA?)。只需决定您的应用程序将支持哪些编译器,列出它们的 #defines,并根据需要更新您的头文件。
回答by Wei Shen
If you would like to dump all available features on a particular platform, you could run GCC like:
如果您想转储特定平台上的所有可用功能,您可以像这样运行 GCC:
gcc -march=native -dM -E - </dev/null
It would dump macros like #define __SSE3__ 1
, #define __AES__ 1
, etc.
这将转储像宏#define __SSE3__ 1
,#define __AES__ 1
等等。
回答by phuclv
If you want a cross-compiler solution then just use Boost.Predef
which contains
如果您想要一个交叉编译器解决方案,那么只需使用Boost.Predef
其中包含
BOOST_ARCH_
for system/CPU architecture one is compiling for.BOOST_COMP_
for the compiler one is using.BOOST_LANG_
for language standards one is compiling against.BOOST_LIB_C_
and BOOST_LIB_STD_ for the C and C++ standard library in use.BOOST_OS_
for the operating system we are compiling to.BOOST_PLAT_
for platforms on top of operating system or compilers.BOOST_ENDIAN_
for endianness of the os and architecture combination.BOOST_HW_
for hardware specific features.BOOST_HW_SIMD
for SIMD (Single Instruction Multiple Data) detection.
BOOST_ARCH_
对于系统/CPU 架构,一个正在编译。BOOST_COMP_
对于正在使用的编译器。BOOST_LANG_
对于语言标准,一个正在编译。BOOST_LIB_C_
和 BOOST_LIB_STD_ 用于正在使用的 C 和 C++ 标准库。BOOST_OS_
对于我们正在编译的操作系统。BOOST_PLAT_
适用于操作系统或编译器之上的平台。BOOST_ENDIAN_
用于操作系统和架构组合的字节序。BOOST_HW_
用于硬件特定功能。BOOST_HW_SIMD
用于 SIMD(单指令多数据)检测。
For example
例如
#if defined(BOOST_ARCH_X86)
#if BOOST_ARCH_X86_64
std::cout << "x86_64 " << BOOST_ARCH_X86_64 << " \n";
#elif BOOST_ARCH_X86_32
std::cout << "x86 " << BOOST_ARCH_X86_32 << " \n";
#endif
#elif defined(BOOST_ARCH_ARM)
#if _M_ARM
std::cout << "ARM " << _M_ARM << " \n";
#elif _M_ARM64
std::cout << "ARM64 " << _M_ARM64 << " \n";
#endif
#endif
You can find out more on how to use it here
回答by Michael Burr
There's nothing standard. Brian Hook documented a bunch of these in his "Portable Open Source Harness", and even tries to make them into something coherent and usable (ymmv regarding that). See the posh.h header on this site:
没有什么标准。Brian Hook 在他的“Portable Open Source Harness”中记录了一堆这些,甚至试图将它们变成连贯和可用的东西(ymmv 对此)。请参阅此站点上的 posh.h 标头:
Note, the link above may require you to enter some bogus userid/password due to a DOS attack some time ago.
请注意,由于前一段时间的 DOS 攻击,上面的链接可能需要您输入一些虚假的用户名/密码。
回答by zvrba
If you need a fine-grained detection of CPU features, the best approach is to ship also a CPUID program which outputs to stdout or some "cpu_config.h" file the set of features supported by the CPU. Then you integrate that program with your build process.
如果您需要对 CPU 功能进行细粒度检测,最好的方法是同时发布一个 CPUID 程序,该程序将 CPU 支持的功能集输出到 stdout 或某些“cpu_config.h”文件。然后将该程序与构建过程集成。