首页 > 编程语言 >C++之libcurl环境配置安装

C++之libcurl环境配置安装

时间:2022-10-05 17:57:13浏览次数:151  
标签:easy libcurl dev vcpkg C++ https curl 安装

目录

C++之libcurl环境配置安装


前言

最近在折腾c++的网络通信, 又想具备跨平台这一特性的, 于是就选了curl这个网络库来使用


curl与libcurl的区别

curl基于libcurl做出来的一个工具, 可以运行在各个平台

libcurl是一个库来的, curl的底层就是对这个库进行封装开发, 如果想在自己的项目中使用网络通信, 还得导入这个库到项目中


Install libcurl on Linux

ubuntu-20.04举例, libcurl有三个包可以选择, 分别为以下三个

libcurl4-gnutls-dev
libcurl4-nss-dev
libcurl4-openssl-dev
  • 这三个包有什么区别呢?
  • 进行https访问时的ssl协议库用的不一样, 分别用的gnutls, nss, openssl

这里选择openssl的安装, 等待安装完成即可使用了~

sudo apt install libcurl4-openssl-dev libcurl4-doc libidn11-dev libkrb5-dev libldap2-dev librtmp-dev libssh2-1-dev libssl-dev zlib1g-dev

如果不选择上述方法安装, 还可以下载源码到本地编译安装

wget https://curl.se/download/curl-7.85.0.tar.gz
tar -vxzf curl-7.85.0.tar.gz
cd curl-7.85.0
./configure --with-openssl
make
make install

具体可以参考 https://github.com/curl/curl/blob/master/docs/INSTALL.md#unix


Install libcurl on Windows

windows平台无论用的是visual studio还是visual studio code 包管理我都推荐用vcpkg

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install libcurl:x64-windows

不指定平台的话默认是x32的, 如果想设置默认平台, 可以添加环境变量export VCPKG_DEFAULT_TRIPLET=x64-windows


源码例子

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2022, 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.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.
 *
 * SPDX-License-Identifier: curl
 *
 ***************************************************************************/
/* <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://www.baidu.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;
}

更多例子可以参考:


其他的网络库

还有很多与curl功能一样的库, 比如: (排名不分先后)

标签:easy,libcurl,dev,vcpkg,C++,https,curl,安装
From: https://www.cnblogs.com/NueXini/p/16756012.html

相关文章

  • MySQL8.0.30安装教程
    第一步:输入MySQL网站       www.mysql.com  选择DOWNLOADS并点击进入:  选择最下面的“MySQL Community(GPL)Downloads>>”选项,进入下一页......
  • 实验4:开源控制器实践——OpenDaylight(附实验环境安装配置)
    一、基本要求1.利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight控制器2.通过Postman工具调用OpenDaylight提供的API下发流表,实现拓扑内主机h1和h3网络中断10s......
  • rocketmq安装
    按照网上教程下载配置环境变量,安装目录不要有空格启动:startmqnamesrv.cmdstartmqbroker.cmd-n127.0.0.1:9876autoCreateTopicEnable=true遇到问题:java11有些参......
  • 已安装python在cmd命令窗口执行python提示“'python' 不是内部或外部命令,也不是可运行
    我的博客这个教程只适合windows,linux不适用,不过话说回来了,linux都是自带python的,所以已经预置好了,只要打python就行了,根本不用加环境变量言归正传写了好长时间的python,......
  • Type Script 配置安装与调试
    1.配置淘宝源npmconfigsetregistryhttps://registry.npm.taobao.org///如果后悔了,想撤销淘宝源就运行 npmconfigdeleteregistry2.安装npminstalltypescrip......
  • Git小乌龟的安装及使用
    https://www.jianshu.com/p/33108325fc871安装git2安装TortoiseGit和语言包3github官网,代码托管4代码开发流程 1切换到master分支 2获取master分支最新代码 3创建子分支 4......
  • Ubuntu安装anaconda后终端前面不显示 base,导入原有环境后报错
    输入下面三条命令即可sourceactivatesourcedeactivatecondaactivateyour-virtual-name设置ssh开机自启(一般会新建文件)sudovimetc/rc.local如果保存报错......
  • Docker 安装Nacos
    一、官方文档https://nacos.io/zh-cn/docs/quick-start-docker.html二、安装1、拉取镜像dockerpullnacos/nacos-server2、创建数据卷映射目录及配置文件mkdir-......
  • SDN 实验环境安装
    ......
  • C++基础神仙速度入门
    ​C++的各部分的重要程度我会标号10星为满分目录C++的基本知识只有个人认为主要的(重要程度:2星):C++的基本框架(重要程度:10星,必背)C++的基本语法(重要程度:9星): C++的基......