You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为ARM交叉编译cppzmq时无法找到sodium.h等头文件

为ARMv7交叉编译依赖cppzmq、Eigen、nlohmann/json的C++应用问题

我有一个C++应用,需要为ARMv7进行交叉编译,依赖三个库:cppzmq、Eigen、nlohmann/json。

现有环境

  • 搭载Debian 9 Buster的树莓派Compute Module 3+
  • Manjaro Linux(也可使用Windows)
$ uname -a
Linux user 5.15.71-1-MANJARO #1 SMP PREEMPT Wed Sep 28 11:24:27 UTC 2022 x86_64 GNU/Linux
  • 基于Meson交叉编译教程搭建的CLion+Meson项目,包含:
    • arm-none-linux-gnueabihf工具链
    • 工具链配置文件raspberry-cm3+.txt
    • 项目配置文件meson.build
    • 依赖管理用的Meson .wrap文件

遇到的问题

这是我第一次做交叉编译和C++开发,目前已成功编译Eigen、Json、catch2、libzmq,但libobsd和libsodium编译完成后未被cppzmq识别,编译时出现头文件缺失错误,清理后的错误日志如下:

Executing subproject cppzmq 

cppzmq| Project name: cppzmq
cppzmq| Project version: 4.8.1
cppzmq| C++ compiler for the host machine: /usr/bin/arm-none-linux-gnueabihf-g++ (gcc 10.3.1 "arm-none-linux-gnueabihf-g++ (GNU Toolchain for the A-profile Architecture 10.3-2021.07 (arm-10.29)) 10.3.1 20210621")
cppzmq| C++ linker for the host machine: /usr/bin/arm-none-linux-gnueabihf-g++ ld.bfd 10.3-2021
cppzmq| C++ compiler for the build machine: c++ (gcc 12.2.0 "c++ (GCC) 12.2.0")
cppzmq| C++ linker for the build machine: c++ ld.bfd 2.39.0
cppzmq| Dependency libzmq from subproject subprojects/zeromq-4.3.4 found: YES undefined
cppzmq| Dependency threads found: YES unknown (cached)
cppzmq| Dependency catch2 from subproject subprojects/Catch2-3.1.0 found: YES 3.1.0
cppzmq| Build targets in project: 149
cppzmq| Subproject cppzmq finished.

meson 0.0.1

  Subprojects
    catch2     : YES
    cppzmq     : YES
    eigen      : YES
    json       : YES
    libobsd    : YES
    libzmq     : YES 2 warnings

  User defined options
    Cross files: /my_path/meson/raspberry-cm3+.txt
    backend    : ninja

Found ninja-1.11.1 at /usr/bin/ninja
Cleaning... 107 files. 
[1/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o 
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_msg.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/msg.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/msg.cpp:31:
../srcdir/subprojects/zeromq-4.3.4/src/compat.hpp:42:10: fatal error: bsd/string.h: No such file or directory
   42 | #include <bsd/string.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
[2/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o 
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_ipc_address.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/ipc_address.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/ipc_address.cpp:31:
../srcdir/subprojects/zeromq-4.3.4/src/compat.hpp:42:10: fatal error: bsd/string.h: No such file or directory
   42 | #include <bsd/string.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.
[3/270] Compiling C++ object subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o
FAILED: subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o 
/usr/bin/arm-none-linux-gnueabihf-g++ -Isubprojects/zeromq-4.3.4/libobjects.a.p -Isubprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4 -I../srcdir/subprojects/zeromq-4.3.4/include -Isubprojects/zeromq-4.3.4/__CMake_build -I../srcdir/subprojects/zeromq-4.3.4/__CMake_build -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -Wnon-virtual-dtor -std=gnu++11 -O0 -g -fPIC -Wno-tautological-constant-compare -O3 -DNDEBUG -DZMQ_CUSTOM_PLATFORM_HPP -D_GNU_SOURCE -D_REENTRANT -D_THREAD_SAFE -MD -MQ subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o -MF subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o.d -o subprojects/zeromq-4.3.4/libobjects.a.p/src_curve_mechanism_base.cpp.o -c ../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.cpp
In file included from ../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.cpp:32:
../srcdir/subprojects/zeromq-4.3.4/src/curve_mechanism_base.hpp:38:10: fatal error: sodium.h: No such file or directory
   38 | #include "sodium.h"
      |          ^~~~~~~~~~
compilation terminated.

配置文件详情

raspberry-cm3+.txt(交叉编译工具链配置)

[binaries]
c = '/usr/bin/arm-none-linux-gnueabihf-gcc'
cpp = '/usr/bin/arm-none-linux-gnueabihf-g++'
ar = '/usr/bin/arm-none-linux-gnueabihf-ar'
strip = '/usr/bin/arm-none-linux-gnueabihf-strip'

[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'armv7hl'
endian = 'little'

meson.build(项目配置)

project('meson', 'cpp', version : '0.0.1')

cpp = meson.get_compiler('cpp')
os = target_machine.system()

cmake = import('cmake')


dep_eigen = subproject('eigen').get_variable('eigen_dep')

lib_zmq = cmake.subproject('libzmq')
dep_zmq = lib_zmq.dependency ('libzmq')

# lib_libsodium = cmake.subproject('libsodium')
# dep_libsodium = lib_libsodium.dependency ('sodium')

# lib_libsodium = cpp.find_library('libsodium', required: false)
#   if lib_libsodium.found()
#     dep_sqlitecpp = lib_libsodium
#    endif

dep_catch = subproject('catch2').get_variable('catch2_dep')
dep_libobsd = subproject('libobsd').get_variable('libobsd_dep')

if cpp.has_header('zmq.hpp')
    # cppzmq是仅头文件依赖,若已存在头文件无需修改
    dep_cppzmq = [] #declare_dependency()
else
    dep_cppzmq = subproject('cppzmq').get_variable('cppzmq_dep')
endif

if cpp.has_header('nlohmann/json.hpp')
    dep_json = [] #declare_dependency()
else
    dep_json = subproject('json').get_variable('nlohmann_json_dep')
endif

deps = [
dep_eigen,
# dep_libsodium,
dep_libobsd,
dep_catch,
  dep_zmq,
  dep_cppzmq,
  dep_json
]

executable(
'demo',
'main.cpp',
dependencies :deps
)

Wrap文件

  • catch2、cppzmq、eigen、libobsd、json均为Meson Wrap仓库默认配置
  • libsodium.wrap(自定义CMake仓库)
[wrap-git]
directory = libsodium-1.0.18
url = https://github.com/robinlinden/libsodium-cmake.git
revision = master
depth = 1
clone-recursive = true

[provide]
libsodium = libsodium_dep
sodium = sodium_dep
  • libzmq.wrap
[wrap-file]
directory = zeromq-4.3.4
source_url = https://github.com/zeromq/libzmq/releases/download/v4.3.4/zeromq-4.3.4.tar.gz
source_filename = zeromq-4.3.4.tar.gz
source_hash = c593001a89f5a85dd2ddf564805deb860e02471171b3f204944857336295c3e5

[provide]
libzmq = libzmq_dep

补充说明

  • 可以切换到CMake等构建工具
  • AUR仓库有另一个工具链,不确定是否能解决问题

更新信息

已调整CMake配置并添加缓存机制,以下是CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)
project(my_project_name)
set(CMAKE_CXX_STANDARD 14)
include(./cmake/CPM.cmake)
CPMAddPackage(
        GITHUB_REPOSITORY gabime/spdlog
        VERSION 1.11.0
)
CPMAddPackage(
        NAME nlohmann_json
        VERSION 3.11.2
        # Git仓库过大,直接下载归档的include目录
        URL https://github.com/nlohmann/json/releases/download/v3.11.2/include.zip
        URL_HASH SHA256=e5c7a9f49a16814be27e4ed0ee900ecd0092bfb7dbfca65b5a421b774dccaaed
)
if (nlohmann_json_ADDED)
    add_library(nlohmann_json INTERFACE IMPORTED)
    target_include_directories(nlohmann_json INTERFACE ${nlohmann_json_SOURCE_DIR}/include)
endif()
set(BUILD_TESTING OFF)
CPMAddPackage(
        NAME Eigen
        GIT_TAG 3.4.0
        GITLAB_REPOSITORY libeigen/eigen
        DOWNLOAD_ONLY YES
)
if(Eigen_ADDED)
    add_library(Eigen INTERFACE IMPORTED)
    target_include_directories(Eigen INTERFACE ${Eigen_SOURCE_DIR})
endif()
CPMAddPackage(
        NAME zeromq
        GITHUB_REPOSITORY zeromq/libzmq
        VERSION 4.3.4
)
CPMAddPackage(
        NAME cppzmq
        GITHUB_REPOSITORY zeromq/cppzmq
        VERSION 4.9.0
)
add_executable(my_binary MySource1.cpp MySource2.cpp)
target_link_libraries(
        my_binary
        PRIVATE nlohmann_json
        cppzmq
        spdlog
        Eigen
    )

以下是CPM.cmake:

set(CPM_DOWNLOAD_VERSION 0.36.0)
if(CPM_SOURCE_CACHE)
    set(CPM_DOWNLOAD_LOCATION "${CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
elseif(DEFINED ENV{CPM_SOURCE_CACHE})
    set(CPM_DOWNLOAD_LOCATION "$ENV{CPM_SOURCE_CACHE}/cpm/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
else()
    set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM_${CPM_DOWNLOAD_VERSION}.cmake")
endif()
# 展开相对路径,若路径包含波浪号(~)则尤为重要
get_filename_component(CPM_DOWNLOAD_LOCATION ${CPM_DOWNLOAD_LOCATION} ABSOLUTE)
if(NOT (EXISTS ${CPM_DOWNLOAD_LOCATION}))
    message(STATUS "Downloading CPM.cmake to ${CPM_DOWNLOAD_LOCATION}")
    file(DOWNLOAD
            https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake
            ${CPM_DOWNLOAD_LOCATION}
            )
endif()
include(${CPM_DOWNLOAD_LOCATION})

内容的提问来源于stack exchange,提问作者The Mark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:00:36