欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

【libcurl】Visual Studio 2017编译和配置libcurl开发环境

程序员文章站 2022-07-14 09:30:39
...

1、libcurl介绍

libcurl是免费的客户端URL传输库,支持DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP。libcurl支持SSL证书, HTTP POST、HTTP PUT、FTP上传、基于HTTP表单的上传、代理、Cookie、用户+口令认证(基本、摘要、NTLM、协商、Kerberos)、文件续传、HTTP代理隧道等!

libcurl是高度可移植的,它构建和工作在许多平台上,包括Solaris、NETBSD、FreeBSD、OpenBSD、Darwin、HPUX、IIX、AIX、TUR64、Linux、UNIX、HUD、Windows、AMIGA、OS/2、BeOs、Mac OS X、ULTRIX、QNX、OpenVMS、RISC OS、Novell NETWORD、DOS等等。

libcurl是免费的,线程安全,IpV6兼容,功能丰富,良好支撑,快速,文档全面,并已被许多知名的,大的和成功的公司使用。

主页地址:https://curl.haxx.se/libcurl/

2、环境

  • Windows 10(1803)
  • Visual Studio 2017(15.7.3)

3、编译libcurl

  • 下载libcurl,地址:https://curl.haxx.se/download.html
    我使用的是curl-7.60.0。
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境

  • 解压文件,进入目录curl-7.60.0,运行buildconf.bat

  • 进入curl-7.60.0目录下的winbuild目录,复制路径
  • 进行x64编译
    在开始菜单中找到Visual Studio 2017文件夹,选择如下:
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
    打开命令窗口如下:
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
    在命令窗口输入如下命令:

    cd D:\ThirdCode\curl-7.60.0\winbuild #根据自己的路径输入
    d:
    nmake /f Makefile.vc mode=static VC=14 MACHINE=x64 DEBUG=no

    我设置的是在vc14下静态release编译。
    编译参数设置根据winbuild目录下BUILD.WINDOWS.txt自行设置,具体参数如下:

    nmake /f Makefile.vc mode=<static or dll> <options>
    
    where <options> is one or many of:
      VC=<6,7,8,9,10,11,12,14,15>    - VC versions
      WITH_DEVEL=<path>              - Paths for the development files (SSL, zlib, etc.)
                                       Defaults to sibbling directory deps: ../deps
                                       Libraries can be fetched at http://windows.php.net/downloads/php-sdk/deps/
                                       Uncompress them into the deps folder.
      WITH_SSL=<dll or static>       - Enable OpenSSL support, DLL or static
      WITH_NGHTTP2=<dll or static>   - Enable HTTP/2 support, DLL or static
      WITH_MBEDTLS=<dll or static>   - Enable mbedTLS support, DLL or static
      WITH_CARES=<dll or static>     - Enable c-ares support, DLL or static
      WITH_ZLIB=<dll or static>      - Enable zlib support, DLL or static
      WITH_SSH2=<dll or static>      - Enable libSSH2 support, DLL or static
      ENABLE_SSPI=<yes or no>        - Enable SSPI support, defaults to yes
      ENABLE_IPV6=<yes or no>        - Enable IPv6, defaults to yes
      ENABLE_IDN=<yes or no>         - Enable use of Windows IDN APIs, defaults to yes
                                       Requires Windows Vista or later
      ENABLE_WINSSL=<yes or no>      - Enable native Windows SSL support, defaults to yes
      GEN_PDB=<yes or no>            - Generate Program Database (debug symbols for release build)
      DEBUG=<yes or no>              - Debug builds
      MACHINE=<x86 or x64>           - Target architecture (default is x86)
      CARES_PATH=<path to cares>     - Custom path for c-ares
      MBEDTLS_PATH=<path to mbedTLS> - Custom path for mbedTLS
      NGHTTP2_PATH=<path to HTTP/2>  - Custom path for nghttp2
      SSH2_PATH=<path to libSSH2>    - Custom path for libSSH2
      SSL_PATH=<path to OpenSSL>     - Custom path for OpenSSL
      ZLIB_PATH=<path to zlib>       - Custom path for zlib

    等待编译结束后,关闭命令窗口。
    编译后的头、静态文件等见curl-7.60.0目录下builds目录
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境

4、在Visual Studio 2017中使用

  • 创建vc++项目
  • 把前面编译的lib和include目录添加至项目,如下
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
  • 把libcurl_a.lib、ws2_32.lib、winmm.lib、wldap32.lib、Crypt32.lib、Normaliz.lib添加至项目,如下
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
  • 预处理器定义添加CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
  • 运行库设置
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境
  • 运行例子
    测试例子:https://curl.haxx.se/libcurl/c/fileupload.html
    例子中需要修改
    CURLINFO_TOTAL_TIME_T(适用curl-7.60.1版本) 改为 CURLINFO_TOTAL_TIME。
    注意修改下图红框内容
    第一红框中为本地文件路径(例如:D:/TestData/1骄傲.mp3);
    第二红框中为目的文件路径(例如:file:///e:/1骄傲.mp3);
    【libcurl】Visual Studio 2017编译和配置libcurl开发环境