linux+length+头文件,Linux下使用curl.h头文件报错解决方法

环境:ubuntu 1604

需要使用C++进行网络通信,选择使用curl库

编译的时候,报了链接错误。

这里记录一下Linux上安装curl的过程。

1. 去这个网站:https://curl.haxx.se/download.html 下载最新的curl压缩包,我这里是7.60

2. 解压:     tar -zxf curl-7.60.0.tar.gz

3. 进入当前目录:    cd curl-7.60.0/

4. 依次输入如下命令:

./configure

make

sudo make install

5. 等待完成即可

6. terminal输入 g++ main.cpp -lcurl,即可看到默认的a.out文件生成啦

一个例子代码:

#include #include #include size_t CurlWrite_CallbackFunc_StdString(void *contents, size_t size, size_t nmemb, std::string *s)

{

size_t newLength = size*nmemb;

size_t oldLength = s->size();

try

{

s->resize(oldLength + newLength);

}

catch(std::bad_alloc &e)

{

//handle memory problem

return 0;

}

std::copy((char*)contents,(char*)contents+newLength,s->begin()+oldLength);

return size*nmemb;

}

int main()

{

CURL *curl;

CURLcode res;

//curl_global_init(CURL_GLOBAL_DEFAULT);

curl = curl_easy_init();

std::string s;

// while (){

//

// }

if(curl)

{

curl_easy_setopt(curl, CURLOPT_URL, "http://47.89.179.202:5000/turn/2");

// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https

// curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWrite_CallbackFunc_StdString);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

/* Perform the request, res will get the return code */

res = curl_easy_perform(curl);

/* Check for errors */

if(res != CURLE_OK)

{

fprintf(stderr, "curl_easy_perform() failed: %s\n",

curl_easy_strerror(res));

}

/* always cleanup */

curl_easy_cleanup(curl);

}

std::cout<

参考资料:

https://curl.haxx.se/docs/install.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值