一、编译前工作

        进入下载页面:https://curl.se/download.html,下载.tar.gz包到指定目录。例如,下载curl-7.76.1.tar.gz并解压到/home/jaron/workspace/curl-7.76.1
        在父目录创建构建目录,例如:/home/jaron/workspace/curl-7.76.1-build,通过终端进入构建目录,输入命令:

1、常规编译

./../curl-7.76.1/configure --prefix=/opt/curl-7.76.1-ubuntu-x64 --with-ssl --enable-tls-srp --enable-versioned-symbols

2、交叉编译

# 设置交叉编译工具链到环境变量
export PATH=/opt/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin:$PATH

# 设置OpenSSL依赖项到环境变量
export OPENSSL_ROOT=/opt/openssl-1.1.1k-raspberrypi-armv7l
export LD_LIBRARY_PATH=$OPENSSL_ROOT/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=$OPENSSL_ROOT/lib/pkgconfig:$PKG_CONFIG_PATH
export PATH=$OPENSSL_ROOT/bin:$PATH
export CPATH=$OPENSSL_ROOT/include:$CPATH
export LIBRARY_PATH=$OPENSSL_ROOT/lib:$LIBRARY_PATH

./../curl-7.76.1/configure --host=arm-linux-gnueabihf --prefix=/opt/curl-7.76.1-raspberrypi-armv7l--with-ssl --enable-tls-srp

3、注意项

  • 如果是进行交叉编译,则需要先把交叉编译工具链添加到环境变量,然后设置--host选项,指定交叉编译工具链命令的前缀,生成的Makefile文件中会将使用到的编译工具补全成arm-linux-gnueabihf-gcc或者arm-linux-gnueabihf-ar等。
  • 如果启动了--with-ssl选项,则必须先安装openssl库,安装步骤参考链接

二、编译安装

        配置成功后,输入命令:make,成功后,输入命令:make install,即可把curl库安装到目录:/opt/curl-7.76.1-ubuntu-x64

三、环境配置

        设置环境变量,编辑/etc/profile文件,末尾添加:

export CURL_ROOT=/opt/curl-7.76.1-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
在这里插入图片描述
        进入控制台后,cdcurl-7.76.1\winbuild目录下,执行命令(具体option选项可参考curl-7.76.1\winbuild\README.md说明文档):

nmake /f Makefile.vc mode=<static or dll> <options>

执行结束后,会在curl-7.76.1\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.libwinmm.libwldap32.lib添加进去。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐