iOS编译cURL库并链接darwinssl,zlib,c-ares库操作步骤

本文详细介绍了如何在iOS环境中编译cURL库,并将其与darwinssl、zlib和c-ares库集成。通过执行配置脚本和特定的编译选项,最终成功编译并验证了库的功能。在创建测试demo时,需要注意从/usr/local/lib/提取库文件,导入security.framework,以及处理Library not loaded的运行时错误。最后,提供了手动编译的注意事项和附录中的参考命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方更新文档(原代码里change文件)
- Secure Transport: no more "darwinssl"
  
  Everyone calls it Secure Transport, now we do too.
  
  Reviewed-by: Nick Zitzmann
  
  Closes #3619

脚本文件(./configure)说明:

  --with-winssl           enable Windows native SSL/TLS
  --without-winssl        disable Windows native SSL/TLS
  --with-schannel         enable Windows native SSL/TLS
  --without-schannel      disable Windows native SSL/TLS
  --with-darwinssl        enable Apple OS native SSL/TLS
  --without-darwinssl     disable Apple OS native SSL/TLS
  --with-secure-transport enable Apple OS native SSL/TLS
  --without-secure-transport
                          disable Apple OS native SSL/TLS
  --with-amissl           enable Amiga native SSL/TLS (AmiSSL)
  --without-amissl        disable Amiga native SSL/TLS (AmiSSL)
  --with-ssl=PATH         Where to look for OpenSSL, PATH points to the SSL
                          installation (default: /usr/local/ssl); when
                          possible, set the PKG_CONFIG_PATH environment
                          variable instead of using this option
  --without-ssl           disable OpenSSL
1. 从https://curl.haxx.se/ 获取curl源代码(使用最新稳定版本,非github仓库上的master)。
通过Clang编译libcurl,以及curl。用于验证libcurl可用性的源代码可从https://curl.haxx.se/libcurl/c/example.html获得,需要验证默认情况下http和https请求是否可正常完成。
 
2. 从https://c-ares.haxx.se/获取 c-ares源代码(同样使用最新稳定版本)。通过Clang编译libcares。从https://www.zlib.net/ 获取zlib源代码。通过Clang编译libcares。
 
3. 在Mac OS X下,通过Clang编译加入zlib、c-ares、darwinssl支持的libcurl,以及curl。

4. 输出的调研报告可为docx或md文档;可由同项目组的同事根据报告快速重复相应的定制化构建过程;需保存详细的构建细节,以便后续基于GN进行相应脚本的定制。

//curl,curllib集成darwinssl,zlib,cares步骤

1.编译zlib,cares源文件
https://c-ares.haxx.se/,https://www.zlib.net/ 获取源代码,并各自在相应源代码文件里编译安装,(darwinssl是macos原生的)
(
	./configure 
	./configure --enable-debug --enable-maintainer-mode (cares编译选项)
	sudo make
	sudo make install
)

2.编译cURL源文件
在https://curl.haxx.se/下载, 打开curl源代码文件,执行
1.    ./buildconf
链接zlib,cares,darwinssl(更名为Secure Transport):
2.    sudo ./configure --with-darwinssl \--enable-ares \--with-zlib \--without-ssl \--enable-static \--disable-shared  \--disable-ldap \--disable-ldaps \--without-ssl2 CC=clang

3. 
(或者sudo ./configure --with-secure-transport \--enable-ares \--with-zlib \--with
out-ssl)
4.     sudo make 
5.     sudo make install 
(sudo make curl_LDFLAGS=-all-static
 (静态库安装以及编译)
sudo make install curl_LDFLAGS=-all-static)
5      .curl -V

成功之后执行(curl -V)输出结果:

curl 7.65.3 (x86_64-apple-darwin18.7.0) libcurl/7.65.3 SecureTransport zlib/1.2.11 c-ares/1.14.0
Release-Date: 2019-07-19
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: AsynchDNS IPv6 Largefile libz NTLM NTLM_WB SSL UnixSockets
可以看到SecureTransport,zlib,cares都已经安装成功。

完成之后,创建测试demo并验证库。
demo环境:从/usr/local/lib/中提取出前面生成的库文件。存放到项目目录下
注意:需要引入系统框架security.framework
在这里插入图片描述在项目中:导入静态库文件,即所有.a后缀文件

点击运行项目此时会报Library not loaded的运行时错误,解决办法:
project -> target -> Signing & Capabilities
把Headened Runtime 删掉
在这里插入图片描述
这时候即可运行项目了。


demo代码:
#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
    
  printf("0.******************打印curl-v信息********************\n");
    char *ver = curl_version();
    printf(ver,"cURL当前版本%c*\n");
    
  printf("1.******************百度https请求返回********************\n");
  CURL *baidus_curl;
  CURLcode baidus_curl_res;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  baidus_curl = curl_easy_init();
  if(baidus_curl) {
    curl_easy_setopt(baidus_curl, CURLOPT_URL, "https://www.baidu.com/");

#ifdef SKIP_PEER_VERIFICATION
    /*
     * If you want to connect to a site who isn't using a certificate that is
     * signed by one of the certs in the CA bundle you have, you can skip the
     * verification of the server's certificate. This makes the connection
     * A LOT LESS SECURE.
     *
     * If you have a CA cert for the server stored someplace else than in the
     * default bundle, then the CURLOPT_CAPATH option might come handy for
     * you.
     */
    curl_easy_setopt(baidus_curl, CURLOPT_SSL_VERIFYPEER, 0L);
#endif

#ifdef SKIP_HOSTNAME_VERIFICATION
    /*
     * If the site you're connecting to uses a different host name that what
     * they have mentioned in their server certificate's commonName (or
     * subjectAltName) fields, libcurl will refuse to connect. You can skip
     * this check, but this will make the connection less secure.
     */
    curl_easy_setopt(baidus_curl, CURLOPT_SSL_VERIFYHOST, 0L);
#endif

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

    /* always cleanup */
    curl_easy_cleanup(baidus_curl);
  }
    
  printf("2.******************百度http请求返回********************\n");
    CURL *baidu_curl;
    CURLcode baidu_curl_res;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    baidu_curl = curl_easy_init();
    if(baidu_curl) {
      curl_easy_setopt(baidu_curl, CURLOPT_URL, "http://www.baidu.com/");

  #ifdef SKIP_PEER_VERIFICATION
      /*
       * If you want to connect to a site who isn't using a certificate that is
       * signed by one of the certs in the CA bundle you have, you can skip the
       * verification of the server's certificate. This makes the connection
       * A LOT LESS SECURE.
       *
       * If you have a CA cert for the server stored someplace else than in the
       * default bundle, then the CURLOPT_CAPATH option might come handy for
       * you.
       */
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  #endif

  #ifdef SKIP_HOSTNAME_VERIFICATION
      /*
       * If the site you're connecting to uses a different host name that what
       * they have mentioned in their server certificate's commonName (or
       * subjectAltName) fields, libcurl will refuse to connect. You can skip
       * this check, but this will make the connection less secure.
       */
      curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  #endif

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

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

    
    printf("3.******************天翼云门户https请求返回********************\n");
      CURL *TYs_curl;
      CURLcode TYs_curl_res;

      curl_global_init(CURL_GLOBAL_DEFAULT);

      TYs_curl = curl_easy_init();
      if(TYs_curl) {
        curl_easy_setopt(TYs_curl, CURLOPT_URL, "https://cloud.189.cn/main.action");

    #ifdef SKIP_PEER_VERIFICATION
        /*
         * If you want to connect to a site who isn't using a certificate that is
         * signed by one of the certs in the CA bundle you have, you can skip the
         * verification of the server's certificate. This makes the connection
         * A LOT LESS SECURE.
         *
         * If you have a CA cert for the server stored someplace else than in the
         * default bundle, then the CURLOPT_CAPATH option might come handy for
         * you.
         */
        curl_easy_setopt(TYs_curl, CURLOPT_SSL_VERIFYPEER, 0L);
    #endif

    #ifdef SKIP_HOSTNAME_VERIFICATION
        /*
         * If the site you're connecting to uses a different host name that what
         * they have mentioned in their server certificate's commonName (or
         * subjectAltName) fields, libcurl will refuse to connect. You can skip
         * this check, but this will make the connection less secure.
         */
        curl_easy_setopt(TYs_curl, CURLOPT_SSL_VERIFYHOST, 0L);
    #endif

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

        /* always cleanup */
        curl_easy_cleanup(TYs_curl);
      }
    
    printf("4.******************天翼云门户http请求返回********************\n");
      CURL *TY_curl;
      CURLcode TY_curl_res;

      curl_global_init(CURL_GLOBAL_DEFAULT);

      TY_curl = curl_easy_init();
      if(TY_curl) {
        curl_easy_setopt(TY_curl, CURLOPT_URL, "http://cloud.189.cn/main.action");

    #ifdef SKIP_PEER_VERIFICATION
        /*
         * If you want to connect to a site who isn't using a certificate that is
         * signed by one of the certs in the CA bundle you have, you can skip the
         * verification of the server's certificate. This makes the connection
         * A LOT LESS SECURE.
         *
         * If you have a CA cert for the server stored someplace else than in the
         * default bundle, then the CURLOPT_CAPATH option might come handy for
         * you.
         */
        curl_easy_setopt(TY_curl, CURLOPT_SSL_VERIFYPEER, 0L);
    #endif

    #ifdef SKIP_HOSTNAME_VERIFICATION
        /*
         * If the site you're connecting to uses a different host name that what
         * they have mentioned in their server certificate's commonName (or
         * subjectAltName) fields, libcurl will refuse to connect. You can skip
         * this check, but this will make the connection less secure.
         */
        curl_easy_setopt(TY_curl, CURLOPT_SSL_VERIFYHOST, 0L);
    #endif

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

        /* always cleanup */
        curl_easy_cleanup(TY_curl);
      }
    
  curl_global_cleanup();
    
    

  return 0;
}

demo输出结果:
0.******************打印curl-v信息********************
libcurl/7.65.3 SecureTransport zlib/1.2.11 c-ares/1.14.0
1.******************百度https请求返回********************
<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
	<meta http-equiv="content-type" content="text/html;charset=utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=Edge">
	<link rel="dns-prefetch" href="//s1.bdstatic.com"/>
	<link rel="dns-prefetch" href="//t1.baidu.com"/>
	<link rel="dns-prefetch" href="//t2.baidu.com"/>
	<link rel="dns-prefetch" href="//t3.baidu.com"/>
	<link rel="dns-prefetch" href="//t10.baidu.com"/>
	<link rel="dns-prefetch" href="//t11.baidu.com"/>
	<link rel="dns-prefetch" href="//t12.baidu.com"/>
	<link rel="dns-prefetch" href="//b1.bdstatic.com"/>
	<title>百度一下,你就知道</title>
	<link href="https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/home/css/index.css" rel="stylesheet" type="text/css" />
	<!--[if lte IE 8]><style index="index" >#content{height:480px\9}#m{top:260px\9}</style><![endif]-->
	<!--[if IE 8]><style index="index" >#u1 a.mnav,#u1 a.mnav:visited{font-family:simsun}</style><![endif]-->
	<script>var hashMatch = document.location.href.match(/#+(.*wd=[^&].+)/);if (hashMatch && hashMatch[0] && hashMatch[1]) {document.location.replace("http://"+location.host+"/s?"+hashMatch[1]);}var ns_c = function(){};</script>
	<script>function h(obj){obj.style.behavior='url(#default#homepage)';var a = obj.setHomePage('//www.baidu.com/');}</script>
	<noscript><meta http-equiv="refresh" content="0; url=/baidu.html?from=noscript"/></noscript>
	<script>window._ASYNC_START=new Date().getTime();</script>
</head>
<body link="#0000cc"><div id="wrapper" style="display:none;"><div id="u"><a href="//www.baidu.com/gaoji/preferences.html"  onmousedown="return user_c({'fm':'set','tab':'setting','login':'0'})">搜索设置</a>|<a id="btop" href="/"  onmousedown="return user_c({'fm':'set','tab':'index','login':'0'})">百度首页</a>|<a id="lb" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" onclick="return false;"  onmousedown="return user_c({'fm':'set','tab':'login'})">登录</a><a href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F"  onmousedown="return user_c({'fm':'set','tab':'reg'})" target="_bl
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值