Linux下使用libcurl下载入门(编译与示例)

62 篇文章 8 订阅
21 篇文章 0 订阅

网上的资料挺多的,我只把我自己用最简单的步骤实现编译源码和调用libcurl.so的方法写出来,供参考。

首先下载源码curl-7.73.0.zip我已经上传到蓝奏云了:https://autumoon.lanzoux.com/iXJiHhup8yj

然后在linux下编译,编译的方法如下。

进入解压后的文件夹,开启终端:

依次执行下面四个命令:

./configure
make
sudo make install (需要输入密码)
make clean (可以不执行,清理临时文件用的)

生成如下文件,则为正常。

使用qtcreator创建工程,填写以下代码。


/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ***************************************************************************/

#include <stdio.h>

#include <curl/curl.h>

/* <DESC>
 * Get a single file from an FTPS server.
 * </DESC>
 */

struct FtpFile {
    const char *filename;
    FILE *stream;
};

static size_t my_fwrite(void *buffer, size_t size, size_t nmemb,
                        void *stream)
{
    struct FtpFile *out=(struct FtpFile *)stream;
    if(out && !out->stream) {
        /* open file for writing */
        out->stream=fopen(out->filename, "wb");
        if(!out->stream)
            return -1; /* failure, can't open file to write */
    }
    return fwrite(buffer, size, nmemb, out->stream);
}


int main(void)
{
    CURL *curl;
    CURLcode res;
    struct FtpFile ftpfile=
    {
        "yourfile.bin", /* name to store the file as if successful */
        NULL
    };

    printf("come in");

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();
    if(curl) {
        /*
     * You better replace the URL with one that works! Note that we use an
     * FTP:// URL with standard explicit FTPS. You can also do FTPS:// URLs if
     * you want to do the rarer kind of transfers: implicit.
     */
        curl_easy_setopt(curl, CURLOPT_URL,
                         "ftp://user@server/home/user/file.txt");
        /* Define our callback to get called when there's data to be written */
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_fwrite);
        /* Set a pointer to our struct to pass to the callback */
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ftpfile);

        /* We activate SSL and we require it for both control and data */
        curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);

        /* Switch on full protocol/debug output */
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);

        /* always cleanup */
        curl_easy_cleanup(curl);

        if(CURLE_OK != res) {
            /* we failed */
            fprintf(stderr, "curl told us %d\n", res);
        }
    }

    if(ftpfile.stream)
        fclose(ftpfile.stream); /* close the local file */

    curl_global_cleanup();

    return 0;
}

修改代码中的

//author:autumoon
//联系QQ:4589968
//日期:2020-10-29
"ftp://user@server/home/user/file.txt"

为你想要测试的任意下载地址。

接着将前文提到的文件libcurl.so.4.7.0拷贝到工程目录下,修改.pro文件,添加如下代码:

LIBS += /home/autumoon/QTProject/curlTest/libcurl.so.4.7.0

路径需要按照实际的路径更改,这一步骤很重要,是我自己摸索尝试出来的,网上使用的方法都不行。

网上一般使用的方法是这样的,也可以尝试解决问题:

修改已有的pro文件,添加如下几句:

INCLUDEPATH += /home/ubuntu/camera/camera/
LIBS += -L/home/ubuntu/camera/camera -ltest
INCLUDEPATH添加了项目的目录,以便找到头文件,而LIBS添加的是共享库文件,-L添加目录,-l指定共享库名称。

可能以前的QtCreator使用的那种命令格式,但是我使用的版本比较新(安装文件为qt-opensource-linux-x64-5.14.1.run),这个方法失效,总提示找不到目录或者文件

以下是工程目录的结构:

如果你想使用我的工程直接测试,记住一定要用你自己编译的libcurl.so.4.7.0文件,替换我的工程中的libcurl.so.4.7.0文件,否则很可能导致编译不通过,或者即使编译通过了,程序也无法正常工作!

最后是我的测试工程curlTest的蓝奏云下载地址:https://autumoon.lanzoux.com/iq1uYhup8za

欢迎交流与讨论。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值