文章目录
一、编译前工作
进入下载页面:https://curl.se/download.html,下载.tar.gz包到指定目录。例如,下载curl-8.4.0.tar.gz并解压到/home/jaron/workspace/curl-8.4.0。
在父目录创建构建目录,例如:/home/jaron/workspace/curl-8.4.0-build,通过终端进入构建目录,输入命令:
选项说明:
--enable-static:启用静态库(默认禁用)--enable-shared:启用动态库(默认启用)--enable-versioned-symbols:启用版本符号选项--enable-tls-srp:启用TLS-SRP功能--with-openssl=/opt/openssl-1.1.1k:显式指OpenSSL安装根目录
1、常规编译
./../curl-8.4.0/configure \
--enable-static \
--enable-shared \
--enable-versioned-symbols \
--enable-tls-srp \
--with-openssl=/opt/openssl-1.1.1k-ubuntu-x64 \
--prefix=/opt/curl-8.4.0-ubuntu-x64
2、交叉编译
# 设置交叉编译工具链到环境变量
export PATH=/opt/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:$PATH
./../curl-8.4.0/configure \
--host=arm-linux-gnueabihf \
--enable-static \
--enable-shared \
--enable-versioned-symbols \
--enable-tls-srp \
--with-openssl=/opt/openssl-1.1.1k-raspberrypi-armv7l \
--prefix=/opt/curl-8.4.0-raspberrypi-armv7l \
# 手动指定 OpenSSL 头文件和库路径(兜底)
CPPFLAGS="-I/opt/openssl-1.1.1k-raspberrypi-armv7l/include" \
LDFLAGS="-L/opt/openssl-1.1.1k-raspberrypi-armv7l/lib -ldl" \ # -ldl 是 OpenSSL 依赖的动态库加载器
# 强制链接 OpenSSL 库
LIBS="-lcrypto -lssl"
3、注意项
- 如果是进行交叉编译,则需要先把交叉编译工具链添加到环境变量,然后设置
--host选项,指定交叉编译工具链命令的前缀,生成的Makefile文件中会将使用到的编译工具补全成arm-linux-gnueabihf-gcc或者arm-linux-gnueabihf-ar等。 - 如果启动了
--with-openssl选项,则必须先安装openssl库,安装步骤参考链接。
二、编译安装
配置成功后,输入命令:make,成功后,输入命令:make install,即可把curl库安装到目录:/opt/curl-8.4.0-ubuntu-x64。
三、环境配置
设置环境变量,编辑/etc/profile文件,末尾添加:
export CURL_ROOT=/opt/curl-8.4.0-ubuntu-x64
# 运行依赖
export LD_LIBRARY_PATH=$CURL_ROOT/lib:$LD_LIBRARY_PATH
# 开发依赖
export PATH=$CURL_ROOT/bin:$PATH
export CPATH=$CURL_ROOT/include:$CPATH
export LIBRARY_PATH=$CURL_ROOT/lib:$LIBRARY_PATH
export PKG_CONFIG_PATH=$CURL_ROOT/lib/pkgconfig:$PKG_CONFIG_PATH # 需要同步修改pkgconfig目录下libcurl.pc文件中的prefix路径,指定为当前的库路径
四、番外(Windows下编译和环境配置)
1、编译
(1)方法1
通过CMake(cmake-gui)构建Visual Studio工程,然后再打开工程编译出库。此处省略一万字…
(2)方法2
请先在系统中安装Visual Studio 6以上版本,本机使用的是Visual Studio 2019,在开始菜单打开所要编译的架构,如这里选择x86:

进入控制台后,cd到curl-8.4.0\winbuild目录下,执行命令(具体option选项可参考curl-8.4.0\winbuild\README.md说明文档):
nmake /f Makefile.vc mode=<static or dll> <options>
执行结束后,会在curl-8.4.0\builds目录下生成编译结果:


2、环境配置
设如果在Windows下编译安装好了curl,使用CMake构建其他工程时,若需要依赖curl,则需要配置环境变量:

3、常见错误
(1)链接错误:error LNK2001: 无法解析的外部符号 __imp_ldap_init
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_init
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_unbind_s
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_set_option
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_simple_bind_s
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_search_s
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_msgfree
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_err2string
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_first_entry
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_next_entry
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_first_attribute
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_next_attribute
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_get_values_len
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_value_free_len
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_get_dn
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ldap_memfree
1>libcurl.lib(ldap.obj) : error LNK2001: 无法解析的外部符号 __imp_ber_free
方案: 给工程添加依赖的库:项目右键–>属性–>配置属性–>链接器–>输入–>附加依赖项,把 ws2_32.lib、 winmm.lib、 wldap32.lib添加进去。
1092

被折叠的 条评论
为什么被折叠?



