CSerialPort教程4.3.x (7) - CSerialPort在vcpkg中的使用
前言
CSerialPort项目是一个基于C/C++的轻量级开源跨平台串口类库,可以轻松实现跨平台多操作系统的串口读写,同时还支持C#, Java, Python, Node.js等。
CSerialPort项目的开源协议自 V3.0.0.171216 版本后采用GNU Lesser General Public License v3.0
为了让开发者更好的使用CSerialPort进行开发,特编写基于4.3.x版本的CSerialPort教程系列。
CSerialPort项目地址:
本文将介绍如何使用vcpkg依赖包管理工具安装和管理CSerialPort。
1. vcpkg安装CSerialPort
1.1 安装vcpkg
详见 https://github.com/microsoft/vcpkg
1.2 vcpkg安装默认版本CSerialPort
vcpkg安装语法
vcpkg install [packagesname]:triplet
CSerialPort 典型架构安装示例
# x86-windows
vcpkg install cserialport
# x64-windows
vcpkg install cserialport:x64-windows
# x64-windows-static
vcpkg install cserialport:x64-windows-static
# x64-linux
vcpkg install cserialport:x64-linux
# x64-osx
vcpkg install cserialport:x64-osx
vcpkg查看架构
$ vcpkg help triplet
Available architecture triplets
VCPKG built-in triplets:
arm-uwp
arm64-windows
x64-linux
x64-osx
x64-uwp
x64-windows-static
x64-windows
x86-windows
VCPKG community triplets:
...
1.3 vcpkg安装github最新版本的CSerialPort
安装windows x64的github最新版本的CSerialPort
# x64-windows
vcpkg install cserialport:x64-windows --head
1.4 vcpkg安装指定版本的CSerialPort
修改vcpkg\ports\cserialport\portfile.cmake
文件,vcpkg_from_github中修改REF
和SHA512
为指定版本的数据。
REF
为github的commit id, SHA512
为其对应的*.tar.gz
文件的SHA512 hash值。
如以CSerialPort v4.2.0为例
- 找到所需版本的commit id或 tag
$ git log
...
commit 26271e67340bae0ccbd2169bda8032673b329313 (tag: v4.2.0)
...
- 下载并计算该版本*.tar.gz文件的SHA512 hash值
# 通过commit id切换到指定版本
$ wget https://github.com/itas109/CSerialPort/archive/26271e67340bae0ccbd2169bda8032673b329313.tar.gz
# 通过tag切换到指定版本
$ wget https://github.com/itas109/CSerialPort/archive/v4.2.0.tar.gz
# 通过cmake计算SHA512 hash值
$ cmake -E sha512sum 26271e67340bae0ccbd2169bda8032673b329313.tar.gz
58ad41287ba070e1f29936325ba52969fa0b81ac31fcfc4806904c194b8c5ea3bc05ad0ae2fa2a6a9ac00a479739ea6a9559cfae62cd94b41d3c64d79db34130 26271e67340bae0ccbd2169bda8032673b329313.tar.gz
# 通过vcpkg计算SHA512 hash值
$ vcpkg hash v4.2.0.tar.gz
d2220e232b2b870b2692b8724ebeb0804cc3eac4e181ad7181ce0a8afc595cea8633c70c8369639b952d986f525a25e20f14f9fd7e5498b7d0f54392e22a7858
注意:也可以使用vcpkg安装CSerialPort报错时计算的hash值
[DEBUG] Downloading https://github.com/itas109/CSerialPort/archive/26271e67340bae0ccbd2169bda8032673b329313.tar.gz
Error: Failed to download from mirror set:
File does not have the expected hash:
url : [ https://github.com/itas109/CSerialPort/archive/26271e67340bae0ccbd2169bda8032673b329313.tar.gz ]
File path : [ E:\Git\vcpkg\downloads\itas109-CSerialPort-26271e67340bae0ccbd2169bda8032673b329313.tar.gz.17928.part ]
Expected hash : [ 4e0b6d5d07ac9f213762a8bf6a90a109ec134b04a8645dc5fc0b89c69a798c857924ee37f13f421b421148bc39bf1ed4f37361e5d1d9f7f51e0faf01757b3927 ]
Actual hash : [ 58ad41287ba070e1f29936325ba52969fa0b81ac31fcfc4806904c194b8c5ea3bc05ad0ae2fa2a6a9ac00a479739ea6a9559cfae62cd94b41d3c64d79db34130 ]
- 修改vcpkg_from_github中修改
REF
和SHA512
commit id方式
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO itas109/CSerialPort
REF 26271e67340bae0ccbd2169bda8032673b329313
SHA512 d2220e232b2b870b2692b8724ebeb0804cc3eac4e181ad7181ce0a8afc595cea8633c70c8369639b952d986f525a25e20f14f9fd7e5498b7d0f54392e22a7858
HEAD_REF master
)
tag方式
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO itas109/CSerialPort
REF v4.2.0
SHA512 58ad41287ba070e1f29936325ba52969fa0b81ac31fcfc4806904c194b8c5ea3bc05ad0ae2fa2a6a9ac00a479739ea6a9559cfae62cd94b41d3c64d79db34130
HEAD_REF master
)
2. 使用vcpkg安装的CSerialPort
2.1 visual studio非cmake项目
管理员权限 运行
vcpkg integrate install
安装成功后,vs的非cmake项目对于所有已安装的库可直接使用 #include 包含所需头文件且无需额外配置。
2.2 cmake项目
$ cmake -B [build directory] -S . "-DCMAKE_TOOLCHAIN_FILE=[path to vcpkg]/scripts/buildsystems/vcpkg.cmake"
$ cmake --build [build directory]
或
set(CMAKE_TOOLCHAIN_FILE "D:/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
通过find_package查找使用CSerialPort
find_package(CSerialPort QUIET)
if (CSerialPort_FOUND)
include_directories(${CSerialPort_INCLUDE_DIR})
target_link_libraries (${PROJECT_NAME} ${CSerialPort_LIBRARY})
endif()
2.3 直接拷贝vcpkg编译好的库到项目目录
debug版本的bin和lib到debug目录下拷贝
release版本的bin和lib在根目录(vcpkg/installed/x64-windows)拷贝
注意:
vcpkg可以导出已经编译的库
# x64-windows
$ vcpkg export cserialport:x64-windows --zip
License
License under CC BY-NC-ND 4.0: 署名-非商业使用-禁止演绎
Reference:
- https://github.com/itas109/CSerialPort
- https://gitee.com/itas109/CSerialPort
- https://blog.csdn.net/itas109
- https://github.com/microsoft/vcpkg