記一次Windows MinGW g++編譯c++代碼


到這里下載GCC預編譯包:https://sourceforge.net/projects/mingw-w64/files
下載這個:

x86_64 是64位,i686 是32位的意思
posix 是跨平台的意思,win32 僅限Windows
尾綴是指生成的可執行程序和dll所運行的位數,sjlj可以運行在32位也可以運行在64位,seh僅限運行在64位,drawf僅限32位

解壓並配置環境變量

將其內 mingw/bin 目錄配到 PATH 環境變量下,使用命令 g++ -v,得到版本信息:

寫C++代碼

這里用 stl 庫中的 vector 容器。

#include <iostream>
#include <vector>

int main(){
    vector<int> vec1(10, 4);    
    for (int i = 0; i< vec1.size(); i++){
        std::cout << vec1[i] << std::endl;
    }
    system("pause");
    return 0;
}

編譯

g++ .\hello.cpp -o hello

報失敗...

排查原因,是因為 vector 類前要加 std::

#include <iostream>
#include <vector>

int main(){
    std::vector<int> vec1(10, 4);    
    for (int i = 0; i< vec1.size(); i++){
        std::cout << vec1[i] << std::endl;
    }
    system("pause");
    return 0;
}

然后編譯成功了,在 hello.cpp 同級別目錄下生成了 hello.exe 文件

運行

雙擊運行,按理說應該出現10行4,然后等按任意鍵結束,但是報錯:

排查原因,是因為一個動態鏈接庫有問題...

找到g++的動態鏈接庫 mingw/bin/libstdc++-6.dll,放到 hello.exe 旁邊,正常運行:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM