0

Well as the title says

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

Thanks!

zeta
  • 1,113
  • 3
  • 15
  • 24
  • 4
    Do you need to detect if your application is being *compiled* for x86 or x86_64 or if your x86 application is running on a x86_64 machine? Is the distinction you need just x86/x86_64 or you need to distinguish other architectures? Do you need such detection at precompilation-time (to use in #if statements), compile-time (e.g. for templates specialization, array sizes, ...) or runtime? – Matteo Italia Nov 01 '10 at 23:10
  • 2
    Additionally, are you asking about the CPU architecture, the OS architecture (you can run 32-bit Windows on a 64-bit CPU), or a specific process architecture (a 32-bit process can run on 64-bit Windows)? – Chris Schmich Nov 01 '10 at 23:12
  • Why? Stating your requirement for this info might help you get a more useful answer for your scenario. – Steve Townsend Nov 02 '10 at 00:40
  • I have a use case: I'm installing a 32-bit C++ app talking to a 32/64-bit C# DLL. My installer needs to know which flavour to install. – Pierre Feb 02 '17 at 01:02

5 Answers5

5

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).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
3
#if defined(_M_X64)
...
#endif
Andrey
  • 4,216
  • 1
  • 23
  • 31
3

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.

Brendan Long
  • 53,280
  • 21
  • 146
  • 188
1

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

Jim Brissom
  • 31,821
  • 4
  • 39
  • 33
1

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

Uga Buga
  • 1,724
  • 3
  • 19
  • 38