如何为 jsoncpp 编写 cmake 模块?

2024-01-14

我想用jsoncpp用于编写 C++ 代码来解析 JSON 文件。让我解释一下我做了什么。我创建了一个CMakeLists.txt我做了一个FindJsoncpp.cmake以及一个简单的 c++ 文件来测试jsoncpp。当我在不使用 cmake 的情况下编译 C++ 源代码时-I/usr/include/jsoncpp/ -ljsoncpp效果很好。但是当我尝试使用 cmake 构建它时它找不到json.h我包含在我的 C++ 源代码中的头文件。

这是我的CMakeLists.txt:

cmake_minimum_required (VERSION 2.6)
project (Parser)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

include(LibFindMacros)

message("----------- trying to find Jsoncpp-------------")
find_package(Jsoncpp)

if(Jsoncpp_FOUND)
    message("INFO: we found LibJsoncpp on your pc.")
    message(Jsoncpp_FOUND = ${Jsoncpp_FOUND})
    message(Jsoncpp_INCLUDE_DIR = ${Jsoncpp_INCLUDE_DIR}) 
    message(Jsoncpp_LIBRARY = ${Jsoncpp_LIBRARY})
else(Jsoncpp_FOUND)
    message("WARNING: we couldn't find LibJsoncpp on your pc. DLC is disabled.")
endif(Jsoncpp_FOUND)

#set(LIBS ${Jsoncpp_LIBRARY})

# Set the include dir variables and the libraries and let libfind_process do the rest.
# NOTE: Singular variables for this library, plural for libraries this this lib depends on.
set(Jsoncpp_PROCESS_INCLUDES Jsoncpp_INCLUDE_DIR)
set(Jsoncpp_PROCESS_LIBS Jsoncpp_LIBRARY)

# add the executable
add_executable(jsonparser jsonparser.cpp)

这就是FindJsoncpp.cmake我写的:

# - Try to find Jsoncpp
# Once done, this will define
#
#  Jsoncpp_FOUND - system has Jsoncpp
#  Jsoncpp_INCLUDE_DIRS - the Jsoncpp include directories
#  Jsoncpp_LIBRARIES - link these to use Jsoncpp

include(LibFindMacros)

# Use pkg-config to get hints about paths
libfind_pkg_check_modules(Jsoncpp_PKGCONF jsoncpp)

# Include dir
find_path(Jsoncpp_INCLUDE_DIR
  NAMES json/json.h
#  PATHS ./jsoncpp/
  PATHS ${Jsoncpp_PKGCONF_INCLUDE_DIRS} # /usr/include/jsoncpp/json
)

# Finally the library itself
find_library(Jsoncpp_LIBRARY
  NAMES jsoncpp
  PATHS ${Jsoncpp_PKGCONF_LIBRARY_DIRS}
#  PATH ./jsoncpp/
)

set(Jsoncpp_PROCESS_INCLUDES Jsoncpp_INCLUDE_DIR)
set(Jsoncpp_PROCESS_LIBS Jsoncpp_LIBRARY)
libfind_process(Jsoncpp)

最后是一个简单的 C++ 代码,名为jsonparser.cpp测试它:

#include <iostream>
#include <fstream>
#include <json/json.h>
using namespace std;

void printSongInfo(Json::Value song){
    std::clog<<"\n-----------printing a song-------------\n";
    std::clog<<"Name="<<song["name"];
    std::clog<<"Artist="<<song["artist"];
}

int main(){

    std::ifstream catalogFile("catalog.json");

    Json::Value root;   // will contains the root value after parsing.
    Json::Reader reader;
    bool parsingSuccessful = reader.parse( catalogFile, root );
    if ( !parsingSuccessful ){
        // report to the user the failure and their locations in the document.
        std::cout  << "Failed to parse configuration\n"
                   << reader.getFormattedErrorMessages();
        return 1;
    }

    //parsing songs
    const Json::Value songs = root["songs"];
    for ( int index = 0; index < songs.size(); ++index ){  // Iterates over the sequence elements.
       printSongInfo(songs[index] );
    }
    return 0;
}

当我运行jsonparser.cpp使用下面的命令它工作得很好。

g++ -I/usr/include/jsoncpp/ -ljsoncpp jsonparser.cpp

但是当我尝试使用它时cmake我收到此错误:

$~/jsoncppTest/build$ cmake ..
-- The C compiler identification is GNU 4.7.3
-- The CXX compiler identification is GNU 4.7.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
----------- trying to find Jsoncpp-------------
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26") 
-- checking for module 'jsoncpp'
--   found jsoncpp, version 0.6.0
-- Found Jsoncpp 
INFO: we found LibJsoncpp on your pc.
Jsoncpp_FOUND=TRUE
Jsoncpp_INCLUDE_DIR=/usr/include/jsoncpp
Jsoncpp_LIBRARY=/usr/lib/libjsoncpp.so
-- Configuring done
-- Generating done
-- Build files have been written to: ~/jsoncppTest/build
$~/jsoncppTest/build$ make
Scanning dependencies of target jsonparser
[100%] Building CXX object CMakeFiles/jsonparser.dir/jsonparser.cpp.o
~/jsoncppTest/jsonparser.cpp:3:23: fatal error: json/json.h: No such file or directory
compilation terminated.
make[2]: *** [CMakeFiles/jsonparser.dir/jsonparser.cpp.o] Error 1
make[1]: *** [CMakeFiles/jsonparser.dir/all] Error 2
make: *** [all] Error 2

它找不到json/json.h头文件,但之前已经在cmake中创建了jsoncpp库。我检查了jsoncpp.pc文件并发现 ti OK。我不知道我做错了什么!任何帮助,将不胜感激。

我正在使用具有多体系结构支持的 ubuntu 13.04。我听说 64 位编译器存在 jsoncpp 问题,但不知道是否是这样。


好的,我有一个可以在我的系统上编译良好的解决方案。查找 jsoncpp 很棘手,因为 json-c 安装了一个具有相同名称的标头,并且在我的系统上,该标头位于 /usr/include/json/json.h 下。要使其正常工作,您必须进行以下更改:


在 FindJsoncpp.cmake 中:

# Include dir
find_path(Jsoncpp_INCLUDE_DIR
  NAMES json/features.h
  PATH_SUFFIXES jsoncpp
  PATHS ${Jsoncpp_PKGCONF_INCLUDE_DIRS} # /usr/include/jsoncpp/json
)

搜索 json/features.h 而不是 json/json.h 可以避免在我的系统上找到 json-c 的 json.h 文件,该文件不兼容。


在 CMakeLists.txt 中 :

include_directories(${Jsoncpp_INCLUDE_DIR})
add_executable(jsonparser jsonparser.cpp)
target_link_libraries(jsonparser ${Jsoncpp_LIBRARY})

这里设置了找到的目录,因此 CMake 实际上使用它们。


在 jsonparser.cpp 中:

const Json::Value songs = root["songs"];
for ( int index = 0; index < songs.size(); ++index ){  // Iterates over the sequence elements.
   std::clog<<"Name="<<songs[index]["name"];
   std::clog<<"Artist="<<songs[index]["artist"];
}

您的原始代码无法编译,因此我用上面的代码替换了有问题的部分。您是否忘记声明歌曲变量?


我还删除了 getFormattedErrorMessages() 调用,因为我只有 jsoncpp 0.5.0,其中该函数不可用。但这应该没有什么区别。

让我知道这是否适合您。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何为 jsoncpp 编写 cmake 模块? 的相关文章

随机推荐

  • 崩溃并终止原因 0xdead10cc

    请问这起事故的原因是什么 Incident Identifier A176CFB8 6BB7 4515 A4A2 82D2B962E097 CrashReporter Key f02957b828fe4090389c1282ca8e3839
  • Windows Phone 7 上带有盐的 SHA1

    我现在花了一些时间研究如何使用盐将密码编码为 SHA1 这是我在网络应用程序部分使用的代码 但它不适用于电话环境 public class Password private string password private int salt
  • Python unicode:如何测试 unicode 字符串

    我有一个这样的脚本 Python26 coding utf 8 import sys import xlrd import xlwt argset set sys argv 1 import wb xlrd open workbook ex
  • groovy 中的 get 与 getProperty

    令我惊讶的是 根据groovy的文档 groovy可以使用 getProperty 方法来获取对象的属性 因此 当我想更改获取特殊对象属性的行为时 我使用类别类来重写 getProperty 方法 然而 它不起作用 最后 我发现groovy
  • Python SSL:证书验证失败

    我在连接时遇到错误www mydomain com在使用 Windows 8 1 的相当新的机器上使用 Python 2 7 12 错误是SSL CERTIFICATE VERIFY FAILED on the ssl sock conne
  • 分割数据框

    打印 df A B 0 10 1 30 2 50 3 20 4 10 5 30 A B 0 10 1 30 A B 2 50 A B 3 20 4 10 5 30 你可以使用pd cut https pandas pydata org pa
  • 如何使用 C# 使用 DomainName 获取 AD 中的 OU 名称列表?

    我想从 Active Directory 获取 OU 列表 我只有域名 我如何使用 C 实现这一目标 尝试这样的事情 connect to RootDSE to find default naming context DirectoryEn
  • javascript 对 HTML 元素进行就地排序 [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 继续为javascript 对 HT
  • MULTIPART_FORM_DATA:找不到 public javax.ws.rs.core.Response 类型的参数的注入源

    我正在使用基于 Jersey 的 Restful 服务实现策略来构建一个用于上传文件的服务 我的服务类名称是 UploadFileService java 参见下面的代码 package com jerser service import
  • 如何以及何时使用“async”和“await”

    据我了解 主要的事情之一是async and await https learn microsoft com en us dotnet csharp async这样做的目的是使代码易于编写和阅读 但使用它们是否等于生成后台线程来执行长时间的
  • 使用 OpenCV 打开 USB 摄像头

    我想在 Linux Mint 18 3 上使用 C 语言的 OpenCV 打开 USB 摄像头 相机已插入并可与 Common Vision Blocks 的 SoftwareSuite 配合使用 来自命令lsusb我得到以下输出 Bus
  • 为什么要同时使用 os.path.abspath 和 os.path.realpath?

    在多个开源项目中 我见过人们这样做os path abspath os path realpath file 获取当前文件的绝对路径 然而 我发现os path abspath file and os path realpath file
  • 为什么表达式上的 static_cast 具有分布式作用?

    我需要取 2 个无符号 8 位值并减去它们 然后将该值添加到 32 位累加器 8 位减法可能会下溢 但没关系 unsigned int 下溢是已定义的行为 因此没有问题 我希望static cast
  • 为什么我的 CSS 代码不适用于我的 Html 页眉和页脚? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 需要一些帮助 陷入了非常关键的时刻 我必须在明天之前完成这项工作 但我的 CSS 代码不适用于页眉和页脚 而同一文件适用于其他内容 如字体系
  • jOOQ - 与嵌套子查询连接

    假设我想知道是谁写的CLRS在书中数据库 表BOOK AUTHOR带连接表BOOK AUTHOR SelectConditionStep
  • 如何在R语言中展开用户和环境变量?

    有什么方法可以扩展 R 中的波浪号和环境变量吗 例如 在Python中 您可以通过写出以下一组代码来获取它 import os os path expanduser r workspace return Users yourname r w
  • 条件编译的注意事项[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 什么时候进行条件编译是个好主意 什么
  • Vagrant ssh 连接到主机 127.0.0.1:2222 端口 22: 文件号错误

    每当我尝试连接到本地 Vagrant 时 运行时都会收到此错误ssh email protected cdn cgi l email protection 2222来自 Windows git bash ssh connect to hos
  • HTML 5 文件加载图像作为背景图像

    是否可以通过 HTML 5 文件 API 加载图像并使用 javascript jquery 将其设为 css 背景图像 如果可能的话 是如何做到的 dronus 在评论部分发布了这个链接 给了我一个很好的答案 sveinbjorn org
  • 如何为 jsoncpp 编写 cmake 模块?

    我想用jsoncpp用于编写 C 代码来解析 JSON 文件 让我解释一下我做了什么 我创建了一个CMakeLists txt我做了一个FindJsoncpp cmake以及一个简单的 c 文件来测试jsoncpp 当我在不使用 cmake