• 基于libcurl+libopenssl开源库编译出curl下载工具及代码集成curl功能


    准备素材:

    1. openssl的版本: openssl-1.1.1w.tar.gz

    2.curl的版本:curl-8.4.0.tar.gz

    目标:

    1.编译出openssl库;

    2.编译出curl可执行文件及库;

    步骤一:先解压压缩包

    tar -zxvf openssl-1.1.1w.tar.gz

    tar -zxvf curl-8.4.0.tar.gz

    步骤二:编译openssl

    首先需要配置config:

    CC=/home/desheng/asj/ts-sdk/prebuilts/host/gcc/gcc-ts-10.3-2023.2-x86_64-arm-none-linux-uclibcgnueabihf/bin/arm-ts-linux-uclibcgnueabihf-gcc ./config no-asm no-shared no-async --prefix=/home/desheng/asj/libopenssl-out

    CC就是编译器的路径, --prefix就是编译出的库文件及可执行文件存放的位置;no-shared表示编译出静态库

    no-asm
                       Do not use assembler code. This should be viewed as
                       debugging/trouble-shooting option rather than production.
                       On some platforms a small amount of assembler code may
                       still be used even with this option.

      no-async
                       Do not build support for async operations.

    再执行make;由于使用的是32bit的编译器,因此makefile里面产生的-m64需要去掉一下,等待编译完成后再安装,执行make install;

    /*****************************************************************************************************/
    声明:本博内容均由http://blog.csdn.net/edsam49原创,转载请注明出处,谢谢!
    /*****************************************************************************************************/

    步骤三:编译curl库

    首先还是需要配置:

    CPPFLAGS="-I/home/desheng/asj/libopenssl-out/lib/ -I/home/desheng/asj/libopenssl-out/include" LDFLAGS="-L/home/desheng/asj/libopenssl-out/lib" LIBS="-ldl -lssl -lcrypto" ./configure --host=arm-linux CC=/home/desheng/asj/ts-sdk/prebuilts/host/gcc/gcc-ts-10.3-2023.2-x86_64-arm-none-linux-uclibcgnueabihf/bin/arm-ts-linux-uclibcgnueabihf-gcc CXX=/home/desheng/asj/ts-sdk/prebuilts/host/gcc/gcc-ts-10.3-2023.2-x86_64-arm-none-linux-uclibcgnueabihf/bin/arm-ts-linux-uclibcgnueabihf-g++ --with-ssl --enable-static --disable-dict --disable-ftp --disable-imap --disable-ldap --disable-ldaps --disable-pop3 --disable-proxy --disable-rtsp --disable-smtp --disable-telnet --disable-tftp --disable-zlib --without-ca-bundle --without-gnutls --without-libidn --without-librtmp --without-libssh2 --without-nss --without-zlib --prefix=/home/desheng/asj/libcurl-out/

    指定好openssl的头文件,库路径,编译器CC的全路径,CXX也配置上,其他就是一些小配置,--prefix是库文件和可执行文件输出的地方;

    配置好,执行make,再执行make install,等待完成即可;

    步骤四:放到板子上去跑一下curl

    笔者把curl产生的文件都拷贝到SD卡里去,然后去执行,

    这样curl工具我们就制作好,可以直接使用了。

    如果需要编程的来写下载程序的,可以调用curl的接口

    1. int httpsApiDownloadFile(char *strURL, char *strFileName, https_download_progress_cb progress_cb, https_download_writedata_cb writedata_cb)
    2. {
    3. int ret = -1;
    4. if(NULL == strURL) {
    5. return ret;
    6. }
    7. log_d("download %s > %s", strURL, strFileName);
    8. CURL *download_handle;
    9. CURLcode imgresult;
    10. FILE *fp = NULL;
    11. download_handle = curl_easy_init();
    12. if(download_handle) {
    13. fp = fopen(strFileName, "w+");
    14. if( fp == NULL ) {
    15. log_e(" File cannot be opened ! \n");
    16. curl_easy_cleanup(download_handle);
    17. return ret;
    18. }
    19. log_d(" File %s be opened ! \n", strFileName);
    20. curl_easy_setopt(download_handle, CURLOPT_URL, strURL);
    21. curl_easy_setopt(download_handle, CURLOPT_TIMEOUT, 100);
    22. curl_easy_setopt(download_handle, CURLOPT_USE_SSL, CURLUSESSL_ALL);
    23. curl_easy_setopt(download_handle, CURLOPT_SSL_VERIFYHOST, 0);
    24. curl_easy_setopt(download_handle, CURLOPT_SSL_VERIFYPEER, 0);
    25. // curl_easy_setopt(download_handle, CURLOPT_POST, 0);
    26. curl_easy_setopt(download_handle, CURLOPT_WRITEDATA, fp);
    27. curl_easy_setopt(download_handle, CURLOPT_NOSIGNAL, 1);
    28. curl_easy_setopt(download_handle, CURLOPT_FOLLOWLOCATION, 1);
    29. if(writedata_cb != NULL) {
    30. curl_easy_setopt(download_handle, CURLOPT_WRITEFUNCTION, writedata_cb);
    31. } else {
    32. curl_easy_setopt(download_handle, CURLOPT_WRITEFUNCTION, httpsDownloadWriteCb);
    33. }
    34. if(progress_cb != NULL) {
    35. curl_easy_setopt(download_handle, CURLOPT_NOPROGRESS, 0);
    36. curl_easy_setopt(download_handle, CURLOPT_PROGRESSFUNCTION, progress_cb);//设置进度回调函数
    37. } else {
    38. curl_easy_setopt(download_handle, CURLOPT_NOPROGRESS, 1);
    39. }
    40. imgresult = curl_easy_perform(download_handle);
    41. if(CURLE_OK != imgresult) {
    42. log_w("Cannot grab the File! \n");
    43. log_w("curl_easy_perform() failed:(%d) %s\n", imgresult, curl_easy_strerror(imgresult));
    44. ret = -1;
    45. } else {
    46. ret = 0;
    47. }
    48. fflush(fp);
    49. fclose(fp);
    50. }
    51. curl_easy_cleanup(download_handle);
    52. return ret;
    53. }

    用代码调用curl接口来下载,可以实时获取进度,更好掌控一点。当然根据自己项目需要来完成吧!怎么方便怎么来。

  • 相关阅读:
    【Spark】win10配置IDEA、saprk、hadoop和scala
    live555 音视频处理相关文档解读,
    变量的引用
    @Transactional注解在类上还是接口上使用,哪种方式更好?
    软信天成:AI驱动,化解企业数据的隐私之痛
    平衡二叉树
    20220810
    python 协程
    asp.net core automapper的使用
    【006期】用Processing写一个根据音乐变化的黑白格游戏
  • 原文地址:https://blog.csdn.net/sundesheng125/article/details/134483052