首页 > 解决方案 > 安装MinGW后C++编译器不工作?

问题描述

我是 C++ 新手,想设置我的电脑来学习编码。但是在安装了所有的 MinGW 包之后编译器就不能工作了,并且它没有显示出什么问题。我怎样才能让它工作?

我正在使用 Windows 10(64 位)。

已安装所有 MinGW 软件包: 在此处输入图像描述

路径已设置: 在此处输入图像描述

使用g++ -v 测试,没关系,在cmd上显示:

C:\Users\shaun\Documents\cpp>g++ -v 使用内置规范。COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=c:/mingw/bin/../libexec/gcc/mingw32/6.3.0/lto-wrapper.exe 目标:mingw32 配置:../src/gcc-6.3.0/configure -- build=x86_64-pc-linux-gnu --host=mingw32 --with-gmp=/mingw --with-mpfr=/mingw --with-mpc=/mingw --with-isl=/mingw --prefix= /mingw --disable-win32-registry --target=mingw32 --with-arch=i586 --enable-languages=c,c++,objc,obj-c++,fortran,ada --with-pkgversion='MinGW.org GCC-6.3.0-1' --enable-static --enable-shared --enable-threads --with-dwarf2 --disable-sjlj-exceptions --enable-version-specific-runtime-libs --with- libiconv-prefix=/mingw --with-libintl-prefix=/mingw --enable-libstdcxx-debug --with-tune=generic --enable-libgomp --disable-libvtv --enable-nls 线程模型:win32 gcc版本 6.3.0 (MinGW.org GCC-6.3.0-1)

但它不起作用: C:\Users\shaun\Documents\cpp>g++ 1.cpp

C:\Users\shaun\Documents\cpp>g++ 2.cpp

C:\Users\shaun\Documents\cpp>

1.cpp只是一个HelloWorld:

#include <iostream>

int main() 
{
std::cout << "Hello, World!";
return 0;
}

2.cpp是一个简单的循环:

#include <iostream>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n ;
    cout<<"please input the height"<<endl;
    cin >> n; 

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n - i -1; j++)
        {
            cout<<" ";
        }
        for (int j = 0; j <= 2 * i; j++)
        {
            if (j == 0 or j == 2 * i)
                cout<<"*";
            else
                cout<<" ";
        }
        cout<<endl;
    }

    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout<<" ";
        }
        for (int j = 0; j <= 2 * ( n - i - 2 ); j++)
        {
            if (j == 0 or j == 2 * ( n - i - 2 ))
                cout<<"*";
            else
                cout<<" ";
        }
        cout<<endl;
    }
    return 0;
}

标签: c++mingwgnu

解决方案


构建和运行 C++ 程序是一个多步骤的过程:

  1. 编辑代码
  2. 将代码编译成目标文件
  3. 将目标文件(和库)链接到可执行程序
  4. 运行可执行程序。

对于简单的程序(如你的),步骤 2 和 3 可以像你一样组合。

您遇到的问题是您没有执行第 4 步,您只构建可执行文件但从未运行它。

如果您没有明确指定输出文件名,则可执行程序应命名为a.exe,您需要运行它:

> g++ 1.cpp
> a.exe

请注意,当您随后构建时,您将使用新程序2.cpp覆盖。a.exe

如果要将可执行文件命名为其他名称,则需要使用以下-o选项:

> g++ 1.cpp -o 1.exe
> g++ 2.cpp -o 2.exe

现在您有两个不同的程序,1.exe并且2.exe,每个程序都是从不同的源文件创建的。


推荐阅读