前言
维护的一个项目,由于特殊的技术原因,需要横跨Windows, Linux, ARMLinux三个平台去维护,因此每次增加一个三方库都是头痛的事情:每个平台都要添加,每个平台都要重复一遍下载,写配置脚本,编译PC,编译arm的过程,十分枯燥。
之前学过一点vcpkg,vcpkg可以对三方库自动下载->编译->输出libs一条龙,很好用,于是想到vcpkg自动化的能力能不能应用到交叉平台上来?
步骤
实验环境:
Vmware的Ubuntu20.04
交叉编译环境为韦东山的imx6ull开发板加上其附带的交叉编译工具,可以去它们官网->100ASK_IMX6ULL_PRO开发板->开发板资料上下载。
首先确保vcpkg在机器上已经安装并配置好环境变量,安装教程参考:https://www.cnblogs.com/linuxAndMcu/p/14696542.html#_label4_5
vcpkg下载完毕后,找到vcpkg根目录下的triplets/community文件夹,这里是vcpkg存放自定义平台支持的地方。
要支持imx6ull平台,需要编写该平台对应的cmake支持文件,内容如下:
imx6ull.cmake
set(VCPKG_TARGET_ARCHITECTURE arm)
set(VCPKG_CRT_LINKAGE dynamic)
set(VCPKG_LIBRARY_LINKAGE dynamic)
set(VCPKG_CMAKE_SYSTEM_NAME Linux)
set(VCPKG_CHAINLOAD_TOOLCHAIN_FILE /opt/vcpkg/triplets/community/imx6ull_toolchain.cmake)
imx6ull_toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(tools /home/zhangdalin/weidongshan/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot)
set(CMAKE_SYSROOT ${tools}/arm-buildroot-linux-gnueabihf/sysroot)
set(CMAKE_C_COMPILER ${tools}/bin/arm-buildroot-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-buildroot-linux-gnueabihf-g++)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
在community文件夹下放入这两个平台文件,就可以让vcpkg安装对应平台的库了,
vcpkg install paho-mqttpp3:imx6ull
在<vcpkg安装位置>/installed, 就可以找到对应平台下的库,直接CV到项目就能用了。
测试:
~/CodeDraft/vcpkg_practice$ vcpkg remove paho-mqttpp3:imx6ull
The following packages are not installed:
paho-mqttpp3:imx6ull
~/CodeDraft/vcpkg_practice$ vcpkg install paho-mqttpp3:imx6ull
Computing installation plan...
The following packages will be built and installed:
paho-mqttpp3[core,ssl]:imx6ull@1.5.2
Detecting compiler hash for triplet imx6ull...
Compiler found: /home/zhangdalin/weidongshan/100ask_imx6ull-sdk/ToolChain/arm-buildroot-linux-gnueabihf_sdk-buildroot/bin/arm-buildroot-linux-gnueabihf-g++
Restored 1 package(s) from /home/zhangdalin/.cache/vcpkg/archives in 58.5 ms. Use --debug to see more details.
Installing 1/1 paho-mqttpp3[core,ssl]:imx6ull@1.5.2...
Elapsed time to handle paho-mqttpp3:imx6ull: 2.29 ms
paho-mqttpp3:imx6ull package ABI: 2e61e2503eb20db571b01c32263f124f8f59ee22638d5a16a21f989eac954767
Total install time: 3.72 ms
Installed contents are licensed to you by owners. Microsoft is not responsible for, nor does it grant any licenses to, third-party packages.
Packages installed in this vcpkg installation declare the following licenses:
EPL-1.0
paho-mqttpp3 provides CMake targets:
# this is heuristically generated, and may not be correct
find_package(PahoMqttCpp CONFIG REQUIRED)
target_link_libraries(main PRIVATE PahoMqttCpp::paho-mqttpp3 PahoMqttCpp::paho-mqttpp3-shared)
~/CodeDraft/vcpkg_practice$ cd /opt/vcpkg/installed/imx6ull/lib/
/opt/vcpkg/installed/imx6ull/lib$ readelf -h libpaho-mqtt
libpaho-mqtt3a.so libpaho-mqtt3as.so.1 libpaho-mqtt3c.so.1.3.14 libpaho-mqttpp3.so
libpaho-mqtt3a.so.1 libpaho-mqtt3as.so.1.3.14 libpaho-mqtt3cs.so libpaho-mqttpp3.so.1
libpaho-mqtt3a.so.1.3.14 libpaho-mqtt3c.so libpaho-mqtt3cs.so.1 libpaho-mqttpp3.so.1.5.1
libpaho-mqtt3as.so libpaho-mqtt3c.so.1 libpaho-mqtt3cs.so.1.3.14
/opt/vcpkg/installed/imx6ull/lib$ readelf -h libpaho-mqtt3a.so
ELF Header:
Magic: 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00
Class: ELF32
Data: 2's complement, little endian
Version: 1 (current)
OS/ABI: UNIX - System V
ABI Version: 0
Type: DYN (Shared object file)
Machine: ARM
Version: 0x1
Entry point address: 0x29d8
Start of program headers: 52 (bytes into file)
Start of section headers: 175192 (bytes into file)
Flags: 0x5000400, Version5 EABI, hard-float ABI
Size of this header: 52 (bytes)
Size of program headers: 32 (bytes)
Number of program headers: 6
Size of section headers: 40 (bytes)
Number of section headers: 27
Section header string table index: 23
参考
https://stackoverflow.com/questions/58777810/how-to-integrate-vcpkg-in-linux-with-cross-build-toolchain-as-well-as-sysroot
https://download.100ask.net/boards/Nxp/100ask_imx6ull_pro/index.html
https://www.cnblogs.com/linuxAndMcu/p/14696542.html#_label4_5