How can I dynamically get the system architecture?

2019-09-06 07:51发布

Well as the title says

is there any way to get the system architecture within c++?

Thanks!

5条回答
我只想做你的唯一
2楼-- · 2019-09-06 08:10

There's a nice big list here. The macros are different for Visual Studio and GCC, but just check if they're defined with #ifdef.

Something like:

#if defined(_M_IX86) || defined(__i386__)

Should give you GCC, Visual Studio, and several others.

查看更多
不美不萌又怎样
3楼-- · 2019-09-06 08:10
#if defined(_M_X64)
...
#endif
查看更多
Evening l夕情丶
4楼-- · 2019-09-06 08:24

You can always mix in a few lines of inline assembly and call CPUID to identify the CPU your code is executing on. See this paper as for how to do it:

http://www.intel.com/Assets/PDF/appnote/241618.pdf

查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-09-06 08:27

Based on "dynamically" and "Visual C++", I'm going to guess you want to do this at run-time under Windows.

In that case, you can use GetSystemInfo or GetNativeSystemInfo to retrieve some basic information about the system and processor. If you need more information about the processor and specific features it supports, you can use IsProcessorFeaturePresent to find them (though it can be a little awkward for this purpose -- you have to ask about each feature individually, and gives a Boolean answer for each).

查看更多
Anthone
6楼-- · 2019-09-06 08:33

On x64 platforms sizeof(void*) returns 8. On x32 platforms sizeof(void*) returns 4. This should also be cross platform.

查看更多
登录 后发表回答