【cpp/c++ summary 工具】 Hunter 包管理器

查看cmake是否满足

ubuntu@DESKTOP-D7DRBER:~/CODE/mycpp/hunter-simple-master$ cmake --version
cmake version 3.16.3

CMake suite maintained and supported by Kitware (kitware.com/cmake).

安装Hunter

  • 即下载HunterGate.cmakecmake文件夹
> mkdir cmake
> wget https://raw.githubusercontent.com/cpp-pm/gate/master/cmake/HunterGate.cmake -O cmake/HunterGate.cmake

在这里插入图片描述

CmakeList文件

cmake_minimum_required(VERSION 3.2)

include("cmake/HunterGate.cmake") # 其中定义了宏HunterGate,宏类似函数:macro(my_macro arg1 arg2)


# HunterGate(
#     URL "https://github.com/cpp-pm/hunter/archive/v0.23.297.tar.gz"
#     SHA1 "3319fe6a3b08090df7df98dee75134d68e2ef5a3"
# ) # 这个连接下载总有问题,参考https://github.com/abandonware-pjz37/hunter-simple,换了下面的连接

HunterGate(
    URL "https://github.com/ruslo/hunter/archive/v0.23.111.tar.gz"
    SHA1 "4189bbd869abe4eb69b5303d3f916c03d34dc154"
)
project(Foo)

hunter_add_package(Boost COMPONENTS regex)
find_package(Boost CONFIG REQUIRED regex )

add_executable(foo foo.cpp)
target_link_libraries(foo PUBLIC Boost::regex)
  • 类似可通过hunter_add_packagefind_package添加其他依赖:
# https://www.openssl.org/
hunter_add_package(BoringSSL)
find_package(OpenSSL CONFIG REQUIRED)

hunter_add_package(libsecp256k1)
find_package(libsecp256k1 CONFIG REQUIRED)

# https://developers.google.com/protocol-buffers/
hunter_add_package(Protobuf)
find_package(Protobuf CONFIG REQUIRED)

foo.cpp文件

#include <iostream>
#include <string>
#include <boost/regex.hpp>

int main() {
    std::string text = "The quick brown fox jumps over the lazy dog.";
    boost::regex pattern("fox"); // 匹配单词"fox"

    // 查找第一个匹配
    if (boost::regex_search(text, pattern)) {
        std::cout << "Found a match!" << std::endl;
    } else {
        std::cout << "No match found." << std::endl;
    }

    // 查找所有匹配
    boost::sregex_iterator it(text.begin(), text.end(), pattern);
    boost::sregex_iterator end;
    while (it != end) {
        std::cout << "Match: " << it->str() << std::endl;
        ++it;
    }

    return 0;
}

运行Hunter并构建项目

  • cmake -H. -B_builds -DHUNTER_STATUS_DEBUG=ON -DCMAKE_BUILD_TYPE=Release

    • -H.为指定源代码目录为当前目录(.),-B_builds:指定构建输出目录为_builds。启用调试状态并设置构建类型为调试模式。
  • cmake --build _builds --config Release

  • 构建成功:
    在这里插入图片描述

  • 运行结果:
    在这里插入图片描述

  • 仍可使用vscode的cmake插件进行调试:
    在这里插入图片描述

相关错误处理:wsl编程环境的网络配置

  • 网络配置错误可能导致无法下载依赖包:
    在这里插入图片描述

  • 可清除之前的下载内容rm -r /home/ubuntu/.hunter

  • vim ~/.bashrc,并添加以下代码配置

# 192.168.1.63为主机ip
export http_proxy="http://192.168.1.63:7890"
export https_proxy="http://192.168.1.63:7890"
export no_proxy="localhost,192.168.1.63"
  • source ~/.bashrc
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值