安装MinGW后C++编译器不能用?

The C++ compiler doesn't work after MinGW is installed?

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

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

已安装所有 MinGW 软件包: enter image description here

路径已设置: enter image description here

用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++ 程序是一个多步骤过程:

  1. 编辑代码
  2. 将代码编译成目标文件
  3. Link 将目标文件(和库)转换为可执行程序
  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.exe2.exe,每个程序都是从不同的源文件创建的。