如何使用 jsoncpp libary 在 yocto 中编译 git repo?

问题描述

我写了一个在 Yocto 中编译 git repo 的方法。存储库包含用于编译代码的 Makefile。我第一次能够在 Yocto 中编译代码,但是当我在代码添加 jsoncpp 库时,Yocto 无法编译它。它显示一些错误,如

test_1.cpp:5:10: Fatal error: jsoncpp/json/json.h: No such file or directory
|     5 | #include <jsoncpp/json/json.h>
|       |          ^~~~~~~~~~~~~~~~~~~~~

这是我的食谱文件。请建议我编译代码所需的更改。

yocto_test.bb

SUMMARY = "Hello World"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
DEPENDS = "jsoncpp"

SRCREV = "90052737ee51abca69a334a9208e2fec82d78c19"
SRC_URI = " \
    git://github.com/arghyaBiswas05/yocto-test.git \
    "

S = "${workdir}/git/"

do_compile() {
    oe_runmake all
}

# Install binary to final directory /usr/bin
do_install() {
    install -d ${D}${bindir}
    install -m 0755 ${S}test_file ${D}${bindir}
}

这是我的Makefile

all:
    ${CXX} test_1.cpp -o test_file $(shell pkg-config --cflags --libs jsoncpp)

install:
    cp test_file /usr/bin

clean:
    rm -rf test_file

请建议我更改。

解决方法

Makefile 应该使用 CXXFLAGSLDFLAGS,而不是硬编码的 JSONLIB。这两个变量由 Yocto 相应地设置,并且需要能够找到您在 DEPENDS 中的配方提供的文件。

如果我可以建议,使用介子、自动工具或 cmake 可能比手动编写 Makefille 更快。

,

命令 pkg-config --cflags --libs jsoncpp 的输出是什么?如果你从 Yocto 运行它,它会打印什么?您可以通过在 makefile 中添加如下内容来要求您的 makefile 本身打印此信息:

$(info args: $(shell pkg-config --cflags --libs jsoncpp))

它可能会打印一些内容,包括标志 -I/usr/include/jasoncpp。如果是,那么您的 #include 是错误的,因为 -I/usr/include/jasoncpp 加上 jasoncpp/json/json.h 的包含给出了 /usr/include/jasoncpp/jasoncpp/json/json.h 的完整路径,而该文件显然没有不存在。

我认为您的包含很可能应该是 #include <json/json.h>,但是如果不知道 jsoncpp 包是如何安装的、它把它的头文件放在哪里以及它期望什么标志,就没有办法确定。