标签:gcc enable -- c++ 编译 nghttp2 usr
序言
公司最近项目的业务需要,需要使用nghttp2这个开源库。所以从github.com上下载了一个release版本的nghttp2,然后通过源码进行编译安装。
编译和编译出错定位与解决
- 下载源码包: wget https://github.com/nghttp2/nghttp2/releases/download/v1.42.0/nghttp2-1.42.0.tar.xz
- 解压:xz -d nghttp2-1.42.0.tar.xz && tar xf nghttp2-1.42.0.tar
- 生成Makefile: ./configure --prefix=/usr --libdir=/usr/lib64
- 编译:make -j4。说明一下,我的环境是4核的,所以用的"make -j4"。
本以为接下来就要进行“make install”啦。可是,我还是太天真啦(编译报错啦)。错误如下:
make[3]: Leaving directory `/root/nghttp2/nghttp2-1.42.0/src/includes'
make[3]: Entering directory `/root/nghttp2/nghttp2-1.42.0/src'
CXX util.o
CXX http2.o
CXX app_helper.o
CC nghttp2_gzip.o
In file included from /usr/include/c++/4.8.2/array:35:0,
from http2.h:34,
from http2.cc:25:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
In file included from /usr/include/c++/4.8.2/cinttypes:35:0,
from app_helper.h:30,
from app_helper.cc:58:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
In file included from /usr/include/c++/4.8.2/chrono:35:0,
from util.h:46,
from util.cc:25:
/usr/include/c++/4.8.2/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
In file included from http2.h:41:0,
from http2.cc:25:
memchunk.h:452:30: error: invalid suffix "_k" on integer constant
using Memchunk16K = Memchunk<16_k>;
^
CXX nghttp.o
In file included from http2.h:41:0,
from app_helper.cc:60:
memchunk.h:452:30: error: invalid suffix "_k" on integer constant
using Memchunk16K = Memchunk<16_k>;
^
app_helper.cc:469:20: error: invalid suffix "_k" on integer constant
......
从第一个错误可以看出需要编译器支持c++11才行。
查看系统中的已有g++版本:
[root@localhost nghttp2-1.42.0]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
查了下资料,得知gcc 4.8.1开始已经支持c++11啦,详见https://gcc.gnu.org/projects/cxx-status.html#cxx11。所以本机中的gcc 4.8.5是已经支持c++11了的。那为什么编译还会报错呢?从提示信息中可以得知,我们需要设置编译选项"-std=c++11" 或 “-std=gnu++11”。
查看配置脚本configure的帮助信息“./configure --help”。内容如下:
[root@localhost nghttp2-1.42.0]# ./configure --help
`configure' configures nghttp2 1.42.0 to adapt to many kinds of systems.
Usage: ./configure [OPTION]... [VAR=VALUE]...
......
Some influential environment variables:
CC C compiler command
CFLAGS C compiler flags
LDFLAGS linker flags, e.g. -L<lib dir> if you have libraries in a
nonstandard directory <lib dir>
LIBS libraries to pass to the linker, e.g. -l<library>
CPPFLAGS (Objective) C/C++ preprocessor flags, e.g. -I<include dir> if
you have headers in a nonstandard directory <include dir>
CPP C preprocessor
LT_SYS_LIBRARY_PATH
User-defined run-time library search path.
CYTHON the Cython executable
CXX C++ compiler command
CXXFLAGS C++ compiler flags
CXXCPP C++ preprocessor
......
Report bugs to <t-tujikawa@users.sourceforge.net>.
[root@localhost nghttp2-1.42.0]#
从配置脚本的帮助信息中可以看出,可以通过CXXFLAGS变量设置c++的编译选项。
所以修改生成Makefile的命令为“./configure --prefix=/usr --libdir=/usr/lib64 CXXCPP="-std=c++11"”。生成Makefile后再次执行“make -j4”命令编译。
心想,这次该可以了吧。哎,我又想多了。错误信息如下:
make[3]: Nothing to be done for `all'.
make[3]: Leaving directory `/root/nghttp2/nghttp2-1.42.0/src/includes'
make[3]: Entering directory `/root/nghttp2/nghttp2-1.42.0/src'
CXX util.o
CXX http2.o
CC timegm.o
CXX app_helper.o
CC nghttp2_gzip.o
CXX nghttp.o
In file included from util.h:52:0,
from http2.h:40,
from http2.cc:25:
template.h: In constructor ‘nghttp2::DList<T>::DList(nghttp2::DList<T>&&)’:
template.h:93:14: error: ‘exchange’ is not a member of ‘std’
: head{std::exchange(other.head, nullptr)},
^
template.h:94:14: error: ‘exchange’ is not a member of ‘std’
tail{std::exchange(other.tail, nullptr)},
^
template.h:95:13: error: ‘exchange’ is not a member of ‘std’
len{std::exchange(other.len, 0)} {}
^
template.h: In member function ‘nghttp2::DList<T>& nghttp2::DList<T>::operator=(nghttp2::DList<T>&&)’:
template.h:101:12: error: ‘exchange’ is not a member of ‘std’
head = std::exchange(other.head, nullptr);
从错误信息可以看出,在c++的std命名空间中找不到模版类std::exchange。查看下std::exchange模版类的介绍,得知要c++14及其以后才支持std::exchange模版类。
通过gcc官方文档得知c++14要gcc 6.1及其以后的版本才支持。
好吧,看来必须升级本机的gcc版本才行啦。
gcc升级
gcc版本库地址:https://gcc.gnu.org/pub/gcc/releases/
最新版本为gcc-10.2.0,整最新的版本来升级环境中的gcc。
gcc的安装可以参照https://gcc.gnu.org/install/index.html
gcc依赖库升级
gcc编译依赖于开源库gmp、mpfr、mpc。所以编译gcc前需要确保环境中有这些库。
由于gcc我要升级到最新的版本,所以这些依赖库我也用最新的版本。依赖库的下载仓库在https://gcc.gnu.org/pub/gcc/infrastructure/。
另外,这些库只有在编译gcc时有用到,所以我将这些库编译后安装到/tmp/gcc目录下。
编译gmp
- 下载:wget https://gcc.gnu.org/pub/gcc/infrastructure/gmp-6.1.0.tar.bz2
- 解压: tar -xjf gmp-6.1.0.tar.bz2
- 生成Makefile:./configure --disable-shared --enable-static --prefix=/tmp/gcc
- 编译、安装:make && make check && make install
编译mpfr
- 下载:wget https://gcc.gnu.org/pub/gcc/infrastructure/mpfr-3.1.4.tar.bz2
- 解压: tar -xjf mpfr-3.1.4.tar.bz2
- 生成Makefile:./configure --disable-shared --enable-static --prefix=/tmp/gcc
- 编译、安装:make && make check && make install
编译mpc
- 下载:wget https://gcc.gnu.org/pub/gcc/infrastructure/mpc-1.0.3.tar.gz
- 解压: tar -xzf Mac-1.0.3.tar.gz
- 生成Makefile:./configure --disable-shared --enable-static --prefix=/tmp/gcc --with-gmp=/tmp/gcc --with-mpfr=/tmp/gcc
- 编译、安装:make && make check && make install
gcc升级
- 下载源码:wget https://gcc.gnu.org/pub/gcc/releases/gcc-10.2.0/gcc-10.2.0.tar.xz
- 解压:xz -d gcc-10.2.0.tar.xz && tar -xf gcc-10.2.0.tar
- 生成Makefile文件:./configure --with-mpc=/tmp/gcc --disable-shared --disable-bootstrap --enable-languages=c,c++ --enable-lto --enable-threads=posix --enable-tls --disable-multilib
特别说明: 由于我的环境中只安装了64位的glibc库,所以必须设置“–disable-multilib”选项,来禁止gcc支持32位。如果你的环境中同时安装了32位和64位的glibc库,同时想要新编译的gcc同时支持32位和64位程序的编译,则应该将该选项改为“–enable-multilib”。 - 编译和安装:make -j4 && make install
- 验证:输入命令“g++ -v”,输出结果如下。
[root@localhost gcc-10.2.0]# g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --with-mpc=/tmp/gcc --disable-shared --disable-bootstrap --disable-libstdcxx-pch --enable-languages=all --enable-libgomp --enable-lto --enable-threads=posix --enable-tls --with-fpmath=sse --enable-multilib
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC)
[root@localhost gcc-10.2.0]#
至此,由nghttp2引发的gcc的相关升级也完成啦。
再编译nghttp2
现在gcc已经升级成功了,所以现在系统中的g++已经支持c++14啦。下面我接着编译和安装nghttp2。
切换到nghttp2源码目录。
生成Makefile文件: ./configure --prefix=/usr --libdir=/usr/lib64
编译和安装:make -j4 && make install
最终成功在本地环境中通过源码编译方式安装好了nghttp2的库。
附加说明
- 由于我的环境中pkg-config的pc_path是“/usr/lib64/pkgconfig:/usr/shar/pkgconfig”,所以我们必须我在编译nghttp2库时配置了“–prefix=/usr/ --libdir=/usr/lib64”。pc_path查看方法:
[root@localhost gcc-10.2.0]# pkg-config --variable pc_path pkg-config
/usr/lib64/pkgconfig:/usr/share/pkgconfig
[root@localhost gcc-10.2.0]#
标签:gcc,enable,--,c++,编译,nghttp2,usr
来源: https://blog.csdn.net/zxc3590235/article/details/112190481
本站声明:
1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。