下载
从GitHub下载最新版本的 curl 源码:
git clone https://github.com/curl/curl.git
安装cmake
一句话的事情:
sudo apt-get install cmake
编译curl
- 进入curl目录下,创建一个build文件夹,用于存放编译产生的中间文件、动态库、头文件等内容:
cd curl
mkdir build
- 进入build文件夹
cd build
- 通过cmke生成Makefile,注意,cmake的语法是:cmake [路径],
..
表示上一级目录
cmake ..
- 在执行完成后你会发现当前目录下存在一个Makefile文件,你直接make就可以编译了。
make
安装
在编译完成后,可以进行安装,注意要用sudo权限:
sudo make install
它按照的位置信息如下:
-- Install configuration: ""
-- Installing: /usr/local/bin/curl-config
-- Installing: /usr/local/lib/pkgconfig/libcurl.pc
-- Installing: /usr/local/include/curl
-- Installing: /usr/local/include/curl/urlapi.h
-- Installing: /usr/local/include/curl/stdcheaders.h
-- Installing: /usr/local/include/curl/curl.h
-- Installing: /usr/local/include/curl/typecheck-gcc.h
-- Installing: /usr/local/include/curl/system.h
-- Installing: /usr/local/include/curl/multi.h
-- Installing: /usr/local/include/curl/easy.h
-- Installing: /usr/local/include/curl/curlver.h
-- Installing: /usr/local/include/curl/mprintf.h
-- Installing: /usr/local/lib/cmake/CURL/CURLTargets.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLTargets-noconfig.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLConfigVersion.cmake
-- Installing: /usr/local/lib/cmake/CURL/CURLConfig.cmake
-- Installing: /usr/local/lib/libcurl.so
-- Installing: /usr/local/bin/curl
-- Set runtime path of "/usr/local/bin/curl" to ""
最后更新一下系统的链接库信息,注意也要用sudo权限:
sudo ldconfig -v
简单使用
在curl的example随便找一个例子:
- https.c
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) 1998 - 2019, Daniel Stenberg, <[email protected]>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.haxx.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
/* <DESC>
* Simple HTTPS GET
* </DESC>
*/
#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://example.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 */
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;
}
然后随便编写一个Makefile文件,注意需要动态链接curl库 -lcurl
:
- Makefile:
CC=gcc
SRC = $(wildcard *.c */*.c)
OBJS = $(patsubst %.c, %.o, $(SRC))
DEP_FILES := $(patsubst %, .%.d,$(OBJS))
DEP_FILES := $(wildcard $(DEP_FILES))
FLAG = -g -Werror -I. -Iinclude -lpthread -lcurl
TARGET = targets
$(TARGET):$(OBJS)
$(CC) -o $@ $^ $(FLAG)
ifneq ($(DEP_FILES),)
include $(DEP_FILES)
endif
%.o:%.c
$(CC) -o $@ -c $(FLAG) $< -g -MD -MF [email protected]
clean:
rm -rf $(TARGET) $(OBJS)
distclean:
rm -rf $(DEP_FILES)
.PHONY:clean
编译并运行:
013 git:(master) ✗ make
gcc -o https.o -c -g -Werror -I. -Iinclude -lpthread -luv -lcurl https.c -g -MD -MF .https.o.d
gcc -o targets https.o -g -Werror -I. -Iinclude -lpthread -luv -lcurl
➜ 013 git:(master) ✗ ./targets
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="https://www.iana.org/domains/example">More information...</a></p>
</div>
</body>
</html>