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

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

本文关键字:不起作用 编译器 C++ MinGW 安装      更新时间:2023-10-16

我是一个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 目标:明w32 配置: ../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. 将对象文件(和库(链接到可执行程序中
  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,每个程序都是从不同的源文件创建的。