Skip to content

54 vs2017下使用libcurl库

Jinxin Chen edited this page Jan 5, 2018 · 1 revision

54 vs2017下使用libcurl库

下载libcurl

下载地址:

https://curl.haxx.se/download.html

7.57.0版本源码下载地址:

https://curl.haxx.se/download/curl-7.57.0.zip

构建libcurl静态库

将下载的源码解压缩,在vs tools里面找到x64 Native Tools Command Prompt for VS 2017运行并进入libcurl的源码目录下的winbuild目录,运行如下命令构建静态libcurl x64库

nmake /E CC="cl /D_USING_V110_SDK71_ /D_WIN32_WINNT=0x0501" /f Makefile.vc mode=static VC=14 MACHINE=x86 DEBUG=no RTLIBCFG=static

完成之后会在build目录下生成库和头文件:

libcurl-vc14-x64-release-static-ipv6-sspi-winssl

引用libcurl

可以直接在项目的c++ directories->include directories里面加上libcurl-vc14-x64-release-static-ipv6-sspi-winssl/include,linker->input里面加上libcurl-vc14-x64-release-static-ipv6-sspi-winssl/lib

或者将文件复制到vs c++项目中。

然后在 c++ -> preprocessor 中添加: CURL_STATICLIB

然后参照示例程序:

https://curl.haxx.se/libcurl/c/simple.html

调用libcur最简单的get方法,然后编译项目。

编译可能会遇到 unresolved external symbol 的问题

LNK2001 unresolved external symbol __imp__strncpy

可以尝试添加 msvcrt.lib and msvcmrt.lib 到 linker -> input 中,这个在下面的地址有讨论:

https://stackoverflow.com/questions/40230731/unresolved-externals-when-compiling-with-freetype

完成之后就可以顺利build通过了。

简单调用

#include "curl/curl.h"

CURL *curl;
CURLcode res;

curl = curl_easy_init();
if (curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
    /* example.com is redirected, so we tell libcurl to follow redirection */
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);
    /* Check for errors */
    if (res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n",
            curl_easy_strerror(res));

    /* always cleanup */
    curl_easy_cleanup(curl);
}
Clone this wiki locally