linux编译curl(带openssl)

我的操作系统:ubuntu 20.04(理论上只要是linux就行)

从github上下载了curl和openssl1.1.1d(说实话并不知道这个1.1.1d是什么意思),总之,我拉的都是最新的代码。

带openssl的目的是为了能够发送https请求,否则只能发送http请求。

我这边新建了个文件夹叫做curl_test(名字任意),然后把curl和openssl都解压到这个文件夹下。

先编译openssl

进入到openssl目录下,阅读README后发现,编译openssl需要查看INSTALL这个文件,那就去看一下吧。

README:

INSTALL:

查看INSTALL可以发现,在linux下编译,要运行config这个文件,注意在openssl目录下还有个Configure文件,不要搞错了哦。

我在openssl目录下新建了个文件家build,用来存放生成的结果(--prefix=xxxx/build),当然大家可以根据习惯来取名字,或者直接生成到/usr/local目录下等等,都是可以的。

依次执行如下命令即可完成安装

./config --prefix=/home/lenovo/FM/curl_test/openssl-OpenSSL_1_1_1d/build
make
make install

然后来编译curl

首先切换到curl目录下,通过各种查看README啊之类的,最后锁定到要看GIT-INFO:

发现需要先使用如下命令来生成configure(在curl目录下执行即可)

autoreconf -fi

同样新建一个build文件夹,因为要带openssl编译,所以执行如下命令即可成功编译curl。

./configure --prefix=/home/lenovo/FM/curl_test/curl-master/build --with-openssl=/home/lenovo/FM/curl_test/openssl-OpenSSL_1_1_1d/build

make

make install

其中--with-openssl后面跟的路径,是openssl生成的lib、include、bin所在的路径,如下图所示:

编译完成后可以看到curl的build目录下:

通过以上步骤,成功完成了curl的编译(带openssl)。

事不宜迟,来试一个demo吧。

在curl目录下的doc/examples目录下,有个https.c,是一个发送https请求的demo,如下所示:

#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 is not 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 are 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;
}

在当前目录(doc/examples)下新建一个makefile文件,注意m小写,因为当前目录下还有一个Makefile文件。

makefile:

https_test:https.c
	gcc -o $@ $^ -I/home/lenovo/FM/curl_test/curl-master/build/include -L/home/lenovo/FM/curl_test/curl-master/build/lib -lcurl

.PHONY:clean
clean:
	rm -rf https_test

大概是如上这么写的,注意把路径换成自己的路径。

执行make,然后执行./https_test,结果如下图所示,显然,这个curl有一个自己的测试服务器,所以这里给https://example.com/发送https请求可以得到响应。

再提一嘴,如果想要编译arm版的curl,需要在./configure这一行命令中加上:

--host=arm-linux

如果不加这一句,默认编译x86版本的。(我的理解应该没错吧?),

还有呢,就是要指定编译器,也是在./configure这一行命令中加,前提是电脑里有这个编译器,比如:

CC=arm-linux-gnueabihf-gcc

最后,为什么使用--with-openssl这个参数指定了路径就可以找到openssl,而不是指定include路径、lib路径等等的呢?

这是因为,编译curl的时候会去--with-openssl指定路径下的 lib/pkgconfig目录下寻找openssl的 .pc文件,从而进一步得到curl依赖的openssl的include目录、lib目录等等。

因此,如果编译的时候已经指定了--with-openssl路径,却始终报错检测不到openssl,比如我下面这个报错:

configure: error: --with-openssl was given but OpenSSL could not be detected

不妨去看看这些.pc文件里的路径对不对呢!都是踩过的坑啊!

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值