最近因為測試上的需要,想要試試怎樣才可以 print libcurldns cache內容來瞧瞧裡面是怎樣的一個風華世界,第一步自然是要試看看怎樣call libcurl,所以在網路上找了一個libcurl sample code test.c 想來buildCentOS 6的機器上run看看,按照網路上的Sample code寫了如下的test.c,

#include<curl/curl.h>

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

 

typedef struct {

  int statusCode;

  char *body;

} Response;

 

 

size_t static callback(void *ptr, size_t size, size_t nmemb, void *userdata) {

  static int index = 0;

  Response *response = (Response*) userdata;

  memcpy(response->body + index, ptr, nmemb);

  char sample[10];

  strncpy(sample, (char *)ptr, 9);

  sample[9] = '\0';

  printf("got the line begin with: %s\n", sample);

  index += nmemb;

  return size * nmemb;

}

 

int main(int argc, char **argv) {

  CURL *curl = curl_easy_init();

 

  if (curl) {

    char* url = "http://www.ietf.org/rfc/rfc2396.txt";

 

    Response *response = malloc(sizeof(Response));

    response->body = malloc(1024 * 1024);

   

    curl_easy_setopt(curl, CURLOPT_WRITEDATA, response);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);

    curl_easy_setopt(curl, CURLOPT_URL, url);

    CURLcode code = curl_easy_perform(curl);

    if (code == CURLE_WRITE_ERROR) {

      printf("error occurred!");

    } else {

      printf("response code:%d", code);

    }

 

    printf("total bytes:%d", strlen(response->body));

 

    free(response->body);

    free(response);

  }

  return 0;

}

 

接下來按照網路寫的指令去build 一個test 執行檔,

gcc -o test test.c -lcurl

結果GG:

 

“error: curl/curl.h: No such file or directory” 代表在CentOS中沒有libcurl module,所以這時候就得求助萬能的yum install來安裝一下libcurl:

yum install libcurl

但是安裝完後來事發生同樣的錯誤,再上網查一下才發現如果是development用的的話,必須再裝一個libcurl-devel,於是再裝一下:

yum install libcurl-devel

接著再用gccbuild一次就沒用提了,成功的生出test二進位執行檔,然後執行該test 執行檔,列印出簡單的結果:

2018319日星期一

 

arrow
arrow

    jackterrylau 發表在 痞客邦 留言(0) 人氣()