- Hunter 是一个跨平台cpp包管理器,点击查看支持的所有包的列表。
查看cmake是否满足
- 查看cmake是否满足Hunter版本要求:
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.cmake
到cmake
文件夹
> 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_package
和find_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