libcurl windows编译,运行配置

  1. 编译
    官方下载源码,找到winbuild目录。打开vs2013开发人员命令提示工具,编译debug版本:E:\cpp11\curl-7.51.0\winbuild>nmake /f Makefile.vc mode=static VC=12 DEBUG=yes
    编译relase版本去掉DEBUG=yes就可以了;
    builds目录下找到生成的include和lib目录;
  2. 使用
    附加头文件目录;
    链接器加入lib库;
    关键,关键,关键,C/C++预处理器中加入:BUILDING_LIBCURL
  3. 上传文件例子
// ConsoleApplication7.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
    CURL *curl;
    CURLcode res;
    struct stat file_info;
    double speed_upload, total_time;
    FILE *fd;

    fopen_s(&fd, "hello", "rb");
    //fd = fopen("debugit", "rb"); /* open file to upload */
    if (!fd) {

        return 1; /* can't continue */
    }

    /* to get the file size */
    if (fstat(_fileno(fd), &file_info) != 0) {

        return 1; /* can't continue */
    }

    curl = curl_easy_init();
    if (curl) {
        /* upload to this place */
        curl_easy_setopt(curl, CURLOPT_URL,
            "localhost:3000/hello");

        /* tell it to "upload" to the URL */
        curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

        /* set where to read from (on Windows you need to use READFUNCTION too) */
        curl_easy_setopt(curl, CURLOPT_READDATA, fd);

        /* and give the size of the upload (optional) */
        curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
            (curl_off_t)file_info.st_size);

        /* enable verbose for easier tracing */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);
        /* Check for errors */
        if (res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",
                curl_easy_strerror(res));

        }
        else {
            /* now extract transfer info */
            curl_easy_getinfo(curl, CURLINFO_SPEED_UPLOAD, &speed_upload);
            curl_easy_getinfo(curl, CURLINFO_TOTAL_TIME, &total_time);

            fprintf(stderr, "Speed: %.3f bytes/sec during %.3f seconds\n",
                speed_upload, total_time);

        }
        /* always cleanup */
        curl_easy_cleanup(curl);
    }

    system("pause");
    return 0;
}

上传文件:

#include "httpcurl.h"
#include <stdio.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <fcntl.h>

static const int OPEN_FILE_ERROR = 100;
static const int FILE_SIZE_ZERO = 101;

namespace httpcurl {
    struct WriteThis {
        FILE *fd;
        long size;
    };

    static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
    {
        struct WriteThis *pooh = (struct WriteThis *)userp;
        if (size*nmemb < 1) {
            return 0;
        }

        size_t readSize = fread(ptr, size, nmemb, pooh->fd);
        pooh->size -= readSize;
        return readSize;
    }

    int UploadFileSync(const std::string &url, const std::string &filePath)
    {
        CURL *curl;
        CURLcode res;
        struct stat file_info;

        struct WriteThis pooh;
        pooh.fd = fopen(filePath.c_str(), "rb");
        if (!pooh.fd) {
            return OPEN_FILE_ERROR;
        }

        fstat(_fileno(pooh.fd), &file_info);
        pooh.size = file_info.st_size;

        if (pooh.size == 0) {
            return FILE_SIZE_ZERO;
        }

        /* In windows, this will init the winsock stuff */
        res = curl_global_init(CURL_GLOBAL_DEFAULT);
        /* Check for errors */
        if (res != CURLE_OK) {
            return res;
        }

        /* get a curl handle */
        curl = curl_easy_init();
        if (curl) {
            /* First set the URL that is about to receive our POST. */
            curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

            /* Now specify we want to POST data */
            curl_easy_setopt(curl, CURLOPT_POST, 1L);

            /* we want to use our own read function */
            curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);

            /* pointer to pass to our read function */
            curl_easy_setopt(curl, CURLOPT_READDATA, &pooh);

            /* get verbose debug output please */
            curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

            /*
            If you use POST to a HTTP 1.1 server, you can send data without knowing
            the size before starting the POST if you use chunked encoding. You
            enable this by adding a header like "Transfer-Encoding: chunked" with
            CURLOPT_HTTPHEADER. With HTTP 1.0 or without chunked transfer, you must
            specify the size in the request.
            */
#ifdef USE_CHUNKED
            {
                struct curl_slist *chunk = NULL;

                chunk = curl_slist_append(chunk, "Transfer-Encoding: chunked");
                res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
                /* use curl_slist_free_all() after the *perform() call to free this
                list again */
            }
#else
            /* Set the expected POST size. If you want to POST large amounts of data,
            consider CURLOPT_POSTFIELDSIZE_LARGE */
            curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, file_info.st_size);
#endif

#ifdef DISABLE_EXPECT
            /*
            Using POST with HTTP 1.1 implies the use of a "Expect: 100-continue"
            header.  You can disable this header with CURLOPT_HTTPHEADER as usual.
            NOTE: if you want chunked transfer too, you need to combine these two
            since you can only set one list of headers with CURLOPT_HTTPHEADER. */

            /* A less good option would be to enforce HTTP 1.0, but that might also
            have other implications. */
            {
                struct curl_slist *chunk = NULL;

                chunk = curl_slist_append(chunk, "Expect:");
                res = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
                /* use curl_slist_free_all() after the *perform() call to free this
                list again */
            }
#endif

            /* 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);
        }
        curl_global_cleanup();
        fclose(pooh.fd);
        return res;
    }

    std::string GetErrorStr(int err)
    {
        if (err > CURLE_OK && err < CURL_LAST) {
            return curl_easy_strerror((CURLcode)err);
        }

        switch (err) {
        case 0:
            return "success";
            break;
        case OPEN_FILE_ERROR:
            return "open file error";
            break;
        case FILE_SIZE_ZERO:
            return "file size is zero";
            break;
        default:
            return "unkown";
            break;
        }
    }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: libcurl是一个用于传输数据的开源库,同时支持多个协议,如HTTP、FTP、SMTP等。在Windows编译libcurl可以按照以下步骤进行: 1. 下载源代码:访问libcurl的官方网站(https://curl.se/download.html)并下载最新版本的源代码。选择一个适合的压缩包(通常是.tar.gz格式)。解压缩下载的文件到一个合适的目录。 2. 安装编译工具:在Windows编译libcurl需要先安装一些必要的编译工具,如MinGW或Cygwin。 3. 配置环境变量:将MinGW或Cygwin的bin目录路径添加到系统的环境变量中,以确保编译器和其他必要的工具可以在命令行中运行。 4. 打开命令行:打开一个命令行终端,进入libcurl源代码的根目录。 5. 运行配置脚本:运行以下命令来运行配置脚本,并生成Makefile文件: ```bash ./configure --disable-shared ``` 这个命令会配置编译选项,并禁用共享库的编译,只编译静态库。 6. 编译代码:运行以下命令编译libcurl代码: ```bash make ``` 7. 完成编译:等待编译完成,如果一切顺利,会在编译目录下生成libcurl的静态库文件(通常是libcurl.a)。这个文件可以被链接到你的应用程序中。 8. 运行测试:运行以下命令以运行libcurl的测试套件,验证编译的正确性: ```bash make test ``` 9. 安装库文件:如果要将libcurl安装到系统中以供其他项目使用,可以运行以下命令: ```bash make install ``` 这会将libcurl的头文件和静态库文件复制到系统的相应目录中。 上述是一个基本的编译libcurl的过程,具体细节可能因不同的编译环境和版本而有所差异。在实施之前,请先仔细阅读源代码中的文档或参考官方网站上的编译指南以获取更详细的信息。 ### 回答2: libcurl 是一个常用的开源网络库,用于进行网络数据的传输和通信。在 Windows 系统下编译 libcurl 可以通过以下步骤实现。 1. 首先,我们需要下载 libcurl 的源代码。可以从官方网站(https://curl.se/download.html)下载最新稳定版本的源代码。选择与你的操作系统相对应的源代码,下载后解压到一个目录。 2. 接下来,我们需要一个 C 编译器来编译 libcurl。在 Windows 系统上,可以使用 MinGW 或者 MSVC(Microsoft Visual C++)编译器。如果你选择使用 MinGW 编译器,你需要先安装 MinGW 并配置好环境变量。 3. 在命令行窗口中,进入 libcurl 源代码的目录。执行以下命令进入代码目录:`cd path_to_libcurl_source_code`。 4. 接下来,我们需要配置 libcurl编译选项。在命令行窗口中执行以下命令:`mkdir build && cd build`,创建一个 build 目录并进入。 5. 在 build 目录中执行以下命令进行配置:`../configure --with-ssl --prefix=path_to_installation_directory`。这个命令会检查系统环境,并为编译做一些配置,其中 `--with-ssl` 表示编译时支持 SSL,`--prefix` 指定 libcurl 编译后的安装目录。 6. 配置完成后,执行以下命令进行编译:`make`。这个命令会根据配置生成并编译 libcurl 的代码。 7. 编译完成后,执行以下命令进行安装:`make install`。这个命令会将编译好的 libcurl 代码安装到指定的安装目录。 8. 至此,libcurlWindows 系统上的编译完成了。你可以在指定的安装目录中找到编译好的库文件和头文件,然后在你的项目中使用它们。 需要注意的是,编译 libcurl 可能会遇到一些依赖项的问题,比如 SSL 库的依赖。在配置编译过程中,你可能需要下载和安装相关的依赖库,并配置好相应的环境变量。具体的依赖项和配置方法可以参考 libcurl 的官方文档或者相关的教程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值