1.准備工作,安裝ssl庫:
sudo apt-get install openssl sudo apt-get install libssl-dev
2.下載libcurl源代碼:
wget https://curl.haxx.se/download/curl-7.65.3.tar.xz
3.解壓並進入源代碼目錄:
tar xf curl-7.65.3.tar.xz cd curl-7.65.3
4.配置編譯選項:
./configure --prefix=/usr \ --disable-static \ --enable-threaded-resolver \ --with-ca-path=/etc/ssl/certs
5.編譯:
make
6.安裝(需要root權限):
make install && rm -rf docs/examples/.deps && find docs \( -name Makefile\* -o -name \*.1 -o -name \*.3 \) -exec rm {} \; && install -v -d -m755 /usr/share/doc/curl-7.65.3 && cp -v -R docs/* /usr/share/doc/curl-7.65.3
至此,編譯安裝完成。
寫代碼測試一下:
#include <stdio.h> #include <curl/curl.h> int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/"); /* 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); } curl_global_cleanup(); return 0; }
編譯:
gcc hello_https.c -l curl -o hello_https
運行:
./hello_https
運行結果截圖:
附 自定義回調處理響應數據代碼:

#include <stdio.h> #include <curl/curl.h> static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) { if(stream == NULL) return 0; printf("%s", ptr); return size * nmemb; } int main(void) { CURL *curl; CURLcode res; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(curl) { curl_easy_setopt(curl, CURLOPT_URL, "https://www.baidu.com/"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); /* 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); } curl_global_cleanup(); return 0; }