1.准备工具

   1).交叉编译工具
   2).下载libcurl和openssl源代码,我使用的是(openssl-1.0.2o.tar,curl-7.59.0.tar)
   3).查看cpu详细
~ # cat /proc/cpuinfo
Processor       : ARMv7 Processor rev 5 (v7l)
BogoMIPS        : 1196.85
Features        : swp half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt
CPU implementer : 0x41
CPU architecture: 7
CPU variant     : 0x0
CPU part        : 0xc07
CPU revision    : 5

Hardware        : hi3516a
Revision        : 0000
Serial          : 0000000000000000

2.开始编译

openssl

库版本 : openssl-1.0.2o
-march和-D__ARM_MAX_ARCH__的值需要对比cpu信息调整
编译参数:
./Configure –prefix=/home/xt/libopenssl –cross-compile-prefix=arm-hisiv300-linux- no-asm shared linux-armv4 -march=armv7-a -D__ARM_MAX_ARCH__=7


make&&make install

curl

库版本 : curl-7.59.0

编译参数:
env LDFLAGS=-R/home/xt/lib/libopenssl/lib ./configure –prefix=/home/xt/libcurl CC=arm-hisiv300-linux-gcc –host=arm-hisiv300-linux –with-ssl=/home/xt/libopenssl
需要看到对用SLL支持显示为支持状态.

这里显示的no就需要检查前面步骤是否错误了

make&&make install

3.测试代码测试(来自互联网,本人也是用的这段代码测试的)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

static size_t save_response_callback(void *buffer, size_t size, size_t count, void **response)
{
    char *ptr = NULL;
    printf("buffer is %s\n", (char *)buffer);
    ptr = (char *)malloc(count * size + 4);
    memcpy(ptr, buffer, count * size);
    *response = ptr;

    return count;
}

int main(int argc, char *argv[])
{
    CURL *curl;
    CURLcode res;
    char *response = NULL;

    if (argc != 2)
    {
        printf("Usage:file<url>;\n");
        return;
    }

    //curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();

    if (curl != NULL)
    {

#if SSL_CERTIFICATE_VERIFICATION

        // 方法1, 设定一个SSL判别证书, 未测试
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
        curl_easy_setopt(curl, CURLOPT_CAINFO, "cacert.pem"); // TODO: 设置一个证书文件
#else
        // 方法2, 设定为不验证证书和HOST
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif

        curl_easy_setopt(curl, CURLOPT_HEADER, 0); //设置httpheader 解析, 不需要将HTTP头写传入回调函数
        curl_easy_setopt(curl, CURLOPT_URL, argv[1]);

        curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); //显示HTTP状态码

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); // TODO: 打开调试信息

        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //设置允许302  跳转

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &save_response_callback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);

        //curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);       //指定解析到的IP地址格式,多IP才有意义

        curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); //在发起连接前等待的时间
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);        //忽略所有的curl传递给php进行的信号

        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30L); //设置cURL允许执行的最长秒数(下载大文件的时候需要调节)
        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);  //linux多线程情况应注意的设置(防止curl被alarm信号干扰)

        set_share_handle(curl);

        res = curl_easy_perform(curl);
        if (res != CURLE_OK)
        {
            printf("curl_wasy_perform error = %s", curl_easy_strerror(res));
        }
        printf("response<%s>\n", response);

        curl_easy_cleanup(curl);
    }
}

官方例子:https://curl.haxx.se/libcurl/c/https.html

版权声明:本文为xie-tong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/xie-tong/p/9329014.html