调试libcurl的准备事项、环境配置

 

Time: 2024-12-17,安装具有时效性,和版本相关 环境:Ubuntu22.04

由于新版本的 nghttp2 依赖 C++ 20 的特性,因此在 Ubuntu22.04 上默认通过 apt install build-essential 安装的 gcc11 版本不匹配,因此需要安装 gcc 13。

gcc13安装

安装 build-essential

sudo apt install build-essential
  • 安装完检查 /usr/bin/ 下是否有 gcc, g++, gcc-11, g++11

添加 ppa 源

sudo add-apt-repository ppa:ubuntu-toolchain-r/test

然后开始安装,并设定不同版本的优先级

# 1. 安装 gcc13 g++13
sudo apt install gcc-13
sudo apt install g++-13
# 2. 设定优先级
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-11 11
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 13

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-11 11
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 13
# 3. 检查版本是否正确
gcc -v
g++ -v

nghttp2安装

版本: 1.64.0 首先下载源码包

wget https://github.com/nghttp2/nghttp2/releases/download/v1.64.0/nghttp2-1.64.0.tar.bz2

然后

tar xf nghttp2-1.64.0.tar.bz2
./configure 
# ./configure --enable-app 可以加入可执行客户端:(nghttp, nghttpd, nghttpx and h2load)
# nghttp为client,nghttpd为server,nghttpx为reverse proxy
make
make install

PS: 可以通过 sudo apt-get install nghttp 来安装默认源里面的 nghttp,自带客户端支持

libcurl 的安装

版本: 8.11.1

 wget https://curl.se/download/curl-8.11.1.tar.gz

然后

tar -zxvf curl-8.11.1.tar.gz
./configure --prefix="/xx/libcurl/curl/build" --enable-debug --with-nghttp2 --with-openssl --without-libpsl # 使用绝对路径
make
make install

在设置的 prefix 下能看到 /bin /include /lib /share 目录

调试

设置环境变量

export LD_LIBRARY_PATH=/xx/libcurl/curl/build/lib:$LD_LIBRARY_PATH

对测试代码,使用该库

gcc -g -o simple simple.c -L /xx/libcurl/curl/build/lib/ -lcurl -I /xx/libcurl/curl/build/include/

查看是不是用的我们设置的 libcurl 库

ldd simple
# 应该可以看到下面的类似内容
libcurl.so.4 => /xxxxx/curl/build/lib/libcurl.so.xxxx

参考:

  • https://www.cnblogs.com/DHJ151250/p/17990879
  • https://nju04zq.github.io/2015/08/26/%E5%A6%82%E4%BD%95%E8%B0%83%E8%AF%95%E7%B3%BB%E7%BB%9F%E5%BA%93-libcurl/
  • https://nghttp2.org/documentation/package_README.html
  • 配置vscode的调试环境:https://juejin.cn/post/7096074768998203399