如何在 Windows 64 位机器上将 C++ 应用程序编译为 x64 模式?
How to compile c++ application into x64 mode in Windows 64bit machine?
我已经安装了 Windows SDK 7.1 平台工具,并打开了一个名为 Microsoft Windows 7 x64 Debug Build Environment
的终端。现在我想使用 cl.exe
命令编译一个 C++
应用程序(名为 main.cpp
)。编译命令为:
cl main.cpp
然后,输出main.obj
和main.exe
文件。但是当我尝试 运行 main.exe
时,它在 begin computing dist
之后和 end computing dist.
之前崩溃了我认为我应该将应用程序编译成 x64
程序。请问有人可以给点建议吗?
main.cpp
如下:
#include <cstdio>
#include <cstdlib>
#ifdef _MSC_VER
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#include <intrin.h>
#include <windows.h>
#else
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>
#endif
int main(int argc, char **argv) {
int nTrn = 10;
int nTst = 10;
int nDim = 1; // 64 bit
printf("allocate memory for feats.\n");
uint64_t *trn_feat = new uint64_t[nTrn]; // nTrn x nDim
uint64_t *tst_feat = new uint64_t[nTst]; // nTst x nDim
printf("initialize the feats.\n");
for(int i=0; i<nTrn; i++)
trn_feat[i] = (((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);
for(int i=0; i<nTst; i++)
tst_feat[i] = (((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);
printf("allocate memory for dist matrix.\n");
uint64_t *dist = new uint64_t[nTrn*nTst];
#ifdef _MSC_VER
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
#else
struct timeval start, end;
long seconds, useconds;
double mtime;
gettimeofday(&start, NULL);
#endif
printf("begin computing dist.\n");
for(int iter=0; iter<100; iter++) {
for(int i=0; i<nTrn; i++) {
for(int j=0; j<nTst; j++) {
uint64_t n = (trn_feat[i] ^ tst_feat[j]);
#ifdef _MSC_VER
dist[i*nTst + j] = __popcnt64(n);
#else
dist[i*nTst + j] = __builtin_popcountll(n);
#endif
}
}
}
printf("end computing dist.\n");
#ifdef _MSC_VER
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
double mtime = (double)ElapsedMicroseconds.QuadPart;
printf("Average time: %.6f nanoseconds\n", (1e6*mtime)/100/(nTrn*nTst));
#else
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Total elapsed time: %.6f milliseconds\n", mtime);
printf("Average time: %.6f nanoseconds\n", (1e6*mtime)/1000/(nTrn*nTst));
#endif
delete[] trn_feat;
delete[] tst_feat;
delete[] dist;
}
以下是可能的答案:
- 安装
Visual Studio Community 2015
- 从
Visual Studio Group
打开 Visual Studio x64 terminal
cl.exe main.cpp /link /machine:x64
然后输出一个main.exe
到运行main.exe
,有两个必要条件:
- 机器或CPU可以支持
__popcnt64
指令集。查看机器是否支持,参考__cpuid.
- Windows 操作系统是 64 位。
我已经安装了 Windows SDK 7.1 平台工具,并打开了一个名为 Microsoft Windows 7 x64 Debug Build Environment
的终端。现在我想使用 cl.exe
命令编译一个 C++
应用程序(名为 main.cpp
)。编译命令为:
cl main.cpp
然后,输出main.obj
和main.exe
文件。但是当我尝试 运行 main.exe
时,它在 begin computing dist
之后和 end computing dist.
之前崩溃了我认为我应该将应用程序编译成 x64
程序。请问有人可以给点建议吗?
main.cpp
如下:
#include <cstdio>
#include <cstdlib>
#ifdef _MSC_VER
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#include <intrin.h>
#include <windows.h>
#else
#include <stdint.h>
#include <sys/time.h>
#include <unistd.h>
#endif
int main(int argc, char **argv) {
int nTrn = 10;
int nTst = 10;
int nDim = 1; // 64 bit
printf("allocate memory for feats.\n");
uint64_t *trn_feat = new uint64_t[nTrn]; // nTrn x nDim
uint64_t *tst_feat = new uint64_t[nTst]; // nTst x nDim
printf("initialize the feats.\n");
for(int i=0; i<nTrn; i++)
trn_feat[i] = (((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);
for(int i=0; i<nTst; i++)
tst_feat[i] = (((uint64_t) rand() << 0) & 0x000000000000FFFFull) |
(((uint64_t) rand() << 16) & 0x00000000FFFF0000ull) |
(((uint64_t) rand() << 32) & 0x0000FFFF00000000ull) |
(((uint64_t) rand() << 48) & 0xFFFF000000000000ull);
printf("allocate memory for dist matrix.\n");
uint64_t *dist = new uint64_t[nTrn*nTst];
#ifdef _MSC_VER
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
#else
struct timeval start, end;
long seconds, useconds;
double mtime;
gettimeofday(&start, NULL);
#endif
printf("begin computing dist.\n");
for(int iter=0; iter<100; iter++) {
for(int i=0; i<nTrn; i++) {
for(int j=0; j<nTst; j++) {
uint64_t n = (trn_feat[i] ^ tst_feat[j]);
#ifdef _MSC_VER
dist[i*nTst + j] = __popcnt64(n);
#else
dist[i*nTst + j] = __builtin_popcountll(n);
#endif
}
}
}
printf("end computing dist.\n");
#ifdef _MSC_VER
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
double mtime = (double)ElapsedMicroseconds.QuadPart;
printf("Average time: %.6f nanoseconds\n", (1e6*mtime)/100/(nTrn*nTst));
#else
gettimeofday(&end, NULL);
seconds = end.tv_sec - start.tv_sec;
useconds = end.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
printf("Total elapsed time: %.6f milliseconds\n", mtime);
printf("Average time: %.6f nanoseconds\n", (1e6*mtime)/1000/(nTrn*nTst));
#endif
delete[] trn_feat;
delete[] tst_feat;
delete[] dist;
}
以下是可能的答案:
- 安装
Visual Studio Community 2015
- 从
Visual Studio Group
打开 cl.exe main.cpp /link /machine:x64
Visual Studio x64 terminal
然后输出一个main.exe
到运行main.exe
,有两个必要条件:
- 机器或CPU可以支持
__popcnt64
指令集。查看机器是否支持,参考__cpuid. - Windows 操作系统是 64 位。