Install g++ on Windows
使用cygwin
使用Mingw
使用Babun
- 安装
pact install gcc-g++
- 卸载
pact remove gcc-g++ pact remove gcc-core
安装成功后试运行
创建文件hello.cpp
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello,World!\n"<<endl;
cin.get();
return 0;
}
编译
g++ hello.cpp -o Hi
报错
fatal error: stddef.h: No such file or directory compilation terminated.
原因
gcc-core
和gcc-g++
版本不统一
解决
运行pact update gcc-core gcc-g++
参考 Compiling using g++ #191 Compilation error: “stddef.h: No such file or directory”
重新运行,成功。获得文件Hi.exe
运行Hi.exe
./Hi.exe
得到结果
Hello,World!
使用C++11
编译
g++ -std=c++11 hello.cpp -o Hi
报错
In file included from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/ext/string_conversions.h:41:0,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/bits/basic_string.h:5249,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/string:52,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/bits/locale_classes.h:40,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/bits/ios_base.h:41,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/ios:42,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/ostream:38,
from /usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/iostream:39,
from hello.cpp:1:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/cstdlib:126:11: error: ‘::at_quick_exit’ has not been declared
using ::at_quick_exit;
^
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/cstdlib:149:11: error: ‘::quick_exit’ has not been declared
using ::quick_exit;
^
原因
不明,各种pact update
无效。之后重新安装babun
依然是同样的问题
可以参考这篇帖子
解决
下载cygwin
安装程序,使用其安装gcc package
依然无效
最后通过安装gcc --version 5.3.0
解决问题,估计是g++
版本的问题
{ ~ } » g++ --version
g++ (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
{ ~ } » gcc --version
gcc (GCC) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
总结
gcc --version 5.4.0
可能有些问题,使用之前的版本就可以了
可以通过cygwin
安装程序安装旧版本,babun
的pact
似乎无法回溯到旧版本
后记
安装g++
几天后发现g++
自动升级了
{ ~ } » g++ --version ~
g++ (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
但是一切编译都正常。我凌乱了
4.9.2 5.4.0