簡體   English   中英

如何在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.objmain.exe文件。 但是,當我嘗試運行main.exe ,它在begin computing distend 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;

    }

以下是可能的答案:

  1. 安裝Visual Studio Community 2015
  2. Visual Studio Group打開Visual Studio x64 terminal
  3. cl.exe main.cpp /link /machine:x64

然后輸出main.exe

要運行main.exe ,有兩個必要條件:

  1. 機器或CPU可以支持__popcnt64指令集。 要檢查機器是否支持它,請參考__cpuid
  2. Windows操作系統是64位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM