如何判断 C++ 代码是在 32 位还是 64 位系统中编译的?


在 C++ 中,没有直接的方法来检查环境架构。对于 Windows 系统,可以使用两个宏来检查架构。这些宏是 _WIN64 和 _WIN32。当系统为 64 位时,则 _WIN64 将为 1,否则 _WIN32 将为 1。因此我们可以通过宏检查来识别架构。

示例

#include <iostream>
using namespace std;
int main() {
   #ifdef _WIN64
      cout << "This is 64 bit system" << endl;
   #elif _WIN32
      cout << "This is 32 bit system" << endl;
   #endif
}

输出

This is 64 bit system

更新于:30-Jul-2019

881 次浏览

开启您的职业

完成课程,获取认证

立即开始
广告
© . All rights reserved.