源码编译g++

最近项目需求,需要从源码安装g++。

前言

在网络上搜集了大部分的资料,最终还是找到了办法,主要也是基于两处引用,特此感谢并引出:

gcc4.7.4降级编译 - 知乎 (zhihu.com)

此篇文章为主要参考过程。

./md-unwind-support.h:65:47: 错误:dereferencing pointer to incomplete type ‘struct ucontext’的解决方法_error: dereferencing pointer to incomplete type ‘s-CSDN博客

此篇文章主要解决了第一篇文章中未能提到的问题。

具体问题

GCC是GNU提供的linux编译系统,包含多种语言,理论上大部分的linux系统会预装GNU,并包含C++和C编译器,然而笔者遇到了一个抽象问题就是拿到了一个只有C编译器的操作系统!!!

根据官方文档,源码构建GCC需要C++编译器,编译高版本的GCC编译器需要旧版本的C++编译器。当然,官方给出的建议是使用交叉编译或已经编译好的分发安装包(.deb,.rpm)。

但是,官方也提到了,只有4.8以下的版本允许使用ISO C89编译器,那不就可以使用C编译器编译带C++的GCC了?本着死马当活马医的态度,笔者进行了尝试,当然,大部分流程参考第一篇文章。

平台

x86_64。

Centos系统,为了不引起不必要的麻烦,特此不展示系统名称。

初始自带只有C编译器的GCC 7.3.0

编译准备

源码包下载

使用wget命令或直接去官网链接点击下载:

wget ftp.gnu.org/gnu/gcc/gcc-4.7.4/gcc-4.7.4.tar.gz

解压文件,并进入文件目录中,可使用以下命令:

tar -jxvf gcc-4.7.4.tar.gz
cd gcc-4.7.4

依赖项源码包下载

可以使用命令一键下载,也可手动下载,这里推荐一键下载,此处运行的是官方给出的一个下载脚本。

./contrib/download_prerequisites

使用命令下载的好处是下载完后不用解压,直接就在文件中。

开始编译

首先制作makefile并建立项目

mkdir build
cd build 
../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib

按照文章1中内容,修改cfns.h、cfns.gperf、gcc.texi三个文件中的内容。

修改完毕后,执行make命令,进行编译:

make

可以选择在make后面加入-j命令,并行编译,速度更快。

make -j

编译过程中会产生报错,这时就需要参考文章2的方法,在build文件夹中生成的x86_64-unknown-linux-gnu(不同平台可能不同)文件夹中的libgcc中进行修改,再次执行make即可。

#md-unwind-support.h 文件中第56行*uc_的定义:
struct ucontext *uc_=context->cfa;
#改为
struct ucontext_t *uc_=context->cfa;

编译成功!

执行make install,将编译完成的GCC安装到操作系统中。

make install 

执行g++ -v命令,可以看到当前已经成功安装g++,即c++编译器,可以完成高版本的GCC编译了。

重新编译高版本

笔者又下载了GCC 7.3.0版本,使用与上述相同的安装流程,但此次安装带有了C++,至此,完成了GCC的全部安装。

要注意的是,在配置时要取消安装libsanitizer。

../configure --enable-checking=release --enable-languages=c,c++ --disable-multilib --disable-libsanitizer

make过程中,由于部分编译过程所耗费内存较多,因此不建议使用make -j,这会使得全部cpu并行编译,内存叠加,建议使用合适的核心数量并行编译。笔者8G内存,使用了4个核心进行编译。

make -j4

执行make install ,可以看到当前GCC中g++版本为7.3.0了,至此,完成了无g++机器源码编译GCC,在此基础上可源码编译更高版本的GCC。

  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Installing c++/g++ on Windows Disclaimer: This page is being maintained mainly for my students. Use these instructions at your own risk. There is no warranty in any form or shape whatsoever!. There is no guarantee that these instructions are up-to-date. With that understood you may continue with the rest of this page if you choose to accept these terms. This page was last updated on September 13, 2005, but still good as of April 30, 2009. Follow these steps to install g++ (the GNU C++ compiler) for Windows. There is no room for creativity here; you must follow the directions exactly. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:, but you can choose a different one. If you choose a different drive or a different folder, you'll need to adapt the directions below accordingly. Download full.exe, an about 14 megabyte executable, to C:\full.exe by right-clicking on the link. Use Save Link As... or Save Target As... Be sure the browser saves the file as C:\full.exe. Run the downloaded executable. This will install g++ (and a lot of other things that you don't really need) on your hard drive. Go to the C: drive using Windows Explorer and double-click on full.exe. Or, open a DOS window (Start > Programs > Command Prompt), connect to the C: drive using the cd command, and type full. Locate where the bin folder was created for the g++ installation. On my Windows XP machine, it was created in the following path: C:\cygnus\cygwin-b20\H-i586-cygwin32\bin You now should add it to the PATH environment variable. You do that by following: Start -> Control Panel -> System -> Advanced -> Environment Variables At this point you can see the PATH variable either in the User Variables or in the System Variables. Add the g++ path into the PATH variable. You add it to the end of the existing value separated by a semicolon (';'). Make sure that you do not lose the original value. You are just appending more to the end separated by a semicolon. Restart your computer. A Cygnus Solutions entry will appear in your Programs menu, and an icon may appear on your desktop. Don't use them! You will use it using the g++ command on a DOS prompt as explained below. You should now be able to run g++ from a DOS (Command Prompt) window. For example, to compile a file called C:\mine\hello.cpp, connect to the C:\mine folder and enter g++ -g hello.cpp -o hello -lm You'll then be able to run the compiled program by entering hello in the DOS window. If you've installed Emacs as described here, you will also be able to run g++ from Emacs. If, when you do this, Emacs tries to compile with the command make -k, you made a mistake during the Emacs installation. If you want to learn how to run g++ on emacs, see here. If you'd like to learn more about where this free compiler came from, we downloaded it from an older site of http://sourceware.org/cygwin/. If you wish to clean up a little, you may delete the file: full.exe at this point. Your g++ compiler is installed under C:\cygnus.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值