问题
Well as the title says
is there any way to get the system architecture within c++?
Thanks!
回答1:
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).
回答2:
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:
#if defined(_M_X64)
...
#endif
回答4:
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:
On x64 platforms sizeof(void*)
returns 8. On x32 platforms sizeof(void*)
returns 4. This should also be cross platform.
来源:https://stackoverflow.com/questions/4073815/how-can-i-dynamically-get-the-system-architecture