c++ - 安装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++ 程序是一个多步骤的过程:
- 编辑代码
- 将代码编译成目标文件
- 将目标文件(和库)链接到可执行程序
- 运行可执行程序。
对于简单的程序(如你的),步骤 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
,每个程序都是从不同的源文件创建的。
推荐阅读
- javascript - 如何使用three.js计算对象的内部边界框
- pytest - 从 pytest-django 使用客户端时如何设置请求
- octobercms - 如何从 OctoberCMS 插件中的事件处理程序重定向?
- python - 如何修复 Seaborn 中的“OSError:从文件初始化失败”错误?
- unity3d - 无法在 Unity 中初始化 Crashlytics/Fabric
- datetime - 如何获取 DateTime.Now ,从偏移量开始
- javascript - 为什么我的代码没有按我预期的方式工作?
- python - 有没有办法使用 python 搜索提取的特征?
- react-native - 如何在 react-native 中按 desc 或 asc 排序?
- datetime - 使用shopify液体模板的时区转换问题