记一次Windows MinGW g++编译c++代码

时间:2020-07-13 09:51:37   收藏:0   阅读:88

到这里下载GCC预编译包:https://sourceforge.net/projects/mingw-w64/files
下载这个:
技术图片

解压并配置环境变量

将其内 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 旁边,正常运行:

技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!