ibcadmin 发表于 2019-10-12 10:22:52

Curl的移植编译以及注意事项

<p>   近来必要用curl来发送http哀求,遇到了不少问题,查了不少资料,都是零零散散的,现在总结下。</p>
<p>   <strong>1、移植编译</strong></p>
<p>      1 ./configure --prefix=$(PWD)/build --host=arm-XXX-linux;make ;make install </p>
<p>      这步基本都没有问题,天生的动态链接库libcurl.a,可以直接给应用步伐去使用。</p>
<p>   <strong> 2、API使用</strong></p>
<p>      1)全局初始化 curl_global_init(CURL_GLOBAL_ALL);</p>
<p>      2)通过curl_easy_init得到一个CURL指针m_pCurl</p>
<p>      3)通过curl_formadd封装参数</p>
<p>          通过curl_easy_setopt设置各种选项</p>
<p>         通过<strong>curl_easy_perform</strong><strong>实行curl的各种操作</strong></p>
<p><strong>   4)curl_easy_getinfo 得到http返回的状态码</strong></p>
<p><strong>   5)curl_easy_cleanup开释CURL指针</strong></p>
<p><strong>   6)curl_global_cleanup开释全局对象</strong></p>
<p><strong><strong>3、http的相应内容获取</strong></strong></p>
<p><strong>   先通过 curl_easy_setopt设置CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA选项</strong></p>

size_t ReviceData(char *buffer, size_t size, size_t nmemb, std::stringstream & stream)
{

    size_t total = size * nmemb;
    std::string str = "";
    if (total)
    {
      str.append(buffer, total);
    }

    stream << str.c_str();

    return total;
}


std::stringstream out;

curl_easy_setopt(m_pCurl, CURLOPT_WRITEFUNCTION, ReviceData);

curl_easy_setopt(m_pCurl, CURLOPT_WRITEDATA, &out);

<p> </p>
<p>    当有相应返来的时间,会触发ReviceData函数,在这个回调函数,会把相应的内容,赋值给out变量,如许相应就取得了。</p>
<p> </p>
<p><strong>   <strong> 4、遇到的一些问题</strong></strong></p>
<p><strong>    curl_easy_setopt(m_pCurl, CURLOPT_CONNECTTIMEOUT, 4L);<br />    curl_easy_setopt(m_pCurl, CURLOPT_TIMEOUT, 25L);</strong></p>
<p><strong>    设置了超时时间后(一个是等候连接的时间,一个是等候吸收相应的时间)一旦域名解析失败,步伐会莫名的挂掉,而且每次挂掉的地方都不一样。当时也是通过一点一点表明代码,才定位到这两行代码。</strong></p>
<p><strong>    coredump的原因是因为curl的DNS解析超时控制是使用SIGALARM实现的。<br />
</strong></p>
<p><strong>    如许导致发现SIGALARM会出现多线程修改同一个全局变量,由此产生了COREDUMP。<br /></strong></p>
<p><strong>    问题发生的条件是设置了CURLOPT_TIMEOUT或CURLOPT_CONNECTTIMEOUT,并且值不为0。</strong></p>
<p><strong> <strong> 办理办法:</strong><br />
1) 设置CURLOPT_NOSIGNAL的值为1</strong></p>
<p><strong>    curl_easy_setopt(m_pCurl, CURLOPT_NOSIGNAL, 1);</strong></p>
<p><strong>   设置之后,发现简直不会coredump了,但是设置的超时时间没有用了,必要等好久,才气出效果。</strong></p>
<p><strong> <br />
2) 使用c-ares(configure时指定参数--enable-ares)</strong></p>
<p><strong>    使用这个方法比力好,不会coredump,而且超时时间设置后收效。</strong></p>
<p><strong>    下面先容下这个方法:</strong></p>
<p><strong>   <strong>a、下载cares的源码,进行编译移植</strong></strong></p>
<p><strong>   https://c-ares.haxx.se/</strong></p>
<p><strong>   编译方法和curl的编译方法类似,都是通过configure ,终极天生libcares.a</strong></p>
<p><strong>    <strong>b、修改curl源码里的configure文件</strong></strong></p>
<p><strong><strong>    </strong></strong> 找到下面的代码,添加embedded_ares="yes",箭头所指的地方,不然check</p>
<p>的时间,会报c-ares library defective or too old</p>
<p><strong>    <div align="center"></div></strong></p>
<p>   c、重新编译curl</p>

./configure --enable-ares=$(PWD)/depends --prefix=$(PWD)/build --host=arm-XXX-linux;make ;make install

<p> </p>
<p>这里指定了enable-ares使用的libares所在的目次。depends目次下必要再建立一个子目次lib,在子目次下放入libcares.a即可。</p>
<p> 还要把libcares的头文件都拷贝到curl主目次里的lib目次里,如许编译就不会堕落了。</p>
<p> </p><br><br/><br/><br/><br/><br/>来源:<a href="https://www.cnblogs.com/ahcc08/p/11656395.html" target="_blank">https://www.cnblogs.com/ahcc08/p/11656395.html</a>
页: [1]
查看完整版本: Curl的移植编译以及注意事项