|Date:||September 6, 2006 / year-entry #302|
|Summary:||Sometimes the questions I see make me shake my head in wonderment. How do I determine programmatically what processor architecture my program was compiled for? I want the x86 version of my program to behave differently from the ia64 version. Is there some API I can call? Note that this person isn't asking whether the...|
Sometimes the questions I see make me shake my head in wonderment.
Note that this person isn't asking whether the program is running on 64-bit Windows. This person wants the program to detect whether it was compiled with an x86 compiler, an ia64 compiler, an amd64 compiler, or whatever.
But why do you need an API for this? You already know what your target architecture is because you compiled it yourself!
It so happens that the Microsoft Visual C++ compiler defines several symbols for you automatically (assuming you're not running the compiler in "strict ANSI compliance mode"). If you're willing to tie yourself to the Microsoft Visual C++ compiler, you can use those symbols.
#ifdef _M_IX86 // the target architecture is x86 #else // the target architecture is something else #endif
If you don't want to tie yourself to a particular compiler,
you'll have to pass that information yourself.
For example, you could have your x86
// assumes you use makefile.def #if defined(_X86_) // the target architecture is x86 #elif defined(_IA64_) // the target architecture is Intel ia64 #elif defined(_AMD64_) // the target architecture is AMD x86-64 #else // some other architecture #endif
As we saw in the earlier article,
you can also use the
But the point is that this is all something you control yourself. You're the one who is compiling the program. You know what your target architecture is. No need to ask somebody to tell you something that is already entirely under your own control.
<-- Back to Old New Thing Archive Index