如何为jsoncpp编写cmake模块? [英] how to write a cmake Module for jsoncpp?

查看:180
本文介绍了如何为jsoncpp编写cmake模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 jsoncpp 编写C ++代码以解析JSON文件.让我解释一下我做了什么.我创建了一个 CMakeLists.txt ,并创建了一个 FindJsoncpp.cmake 以及一个简单的c ++文件来测试jsoncpp.当我使用-I/usr/include/jsoncpp/ -ljsoncpp编译没有cmake的C ++源代码时,它可以正常工作.但是当我尝试使用cmake生成它时,找不到我的c ++源代码中包含的json.h头文件.

I want to use jsoncpp for writing a C++ code in order to parse a JSON file. Let me explain what I did. I created a CMakeLists.txt and I made a FindJsoncpp.cmake along with a simple c++ file to test jsoncpp. When I compile the C++ source without cmake using -I/usr/include/jsoncpp/ -ljsoncpp it works fine. but when I try to build it using cmake it cannot find json.h header file that I included in my c++ source code.

这是我的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)

最后是一个称为jsonparser.cpp的简单C ++代码对其进行测试:

And finally a simple C++ code called jsonparser.cpp to test it:

#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时,它就可以正常工作.

When I run the jsonparser.cpp with below command it works just fine.

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

但是当我尝试使用cmake制作它时,出现此错误:

but when I try to make it using cmake I get this error:

$~/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.我不知道我在做什么错!任何帮助将不胜感激.

It cannot find json/json.h header file but it has founded the jsoncpp library in cmake before. I checked jsoncpp.pc file and found ti OK. I don't know what I am doing wrong! any help would be appreciated.

我正在使用具有多体系结构支持的ubuntu 13.04.我听说过有关64位编译器的jsoncpp问题的信息,但不知道是不是这种情况.

I am using ubuntu 13.04 with multiarch support. I heard something about jsoncpp problem with 64bit compiler but don't know if that's the case.

推荐答案

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

Ok, I have a solution that compiles fine on my system. Finding jsoncpp is tricky, because json-c installs a header with the same name, and on my system, that header is located under /usr/include/json/json.h. To get it work, you have to make the following changes:

:

# 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文件,这是不兼容的.

Searching for json/features.h instead of json/json.h avoids finding the json.h file of json-c on my system, which is not compatible.

:

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

这里找到的目录已建立,因此CMake会实际使用它们.

Here the found directories are set up, so CMake actually uses them.

:

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"];
}

您的原始代码未编译,因此我用上面的代码替换了有问题的代码.您忘了声明歌曲变量了吗?

Your orginal code didn't compile, so I replaced the offending piece with the code above. Have you forgot to declare the song variable?

我还删除了getFormattedErrorMessages()调用,因为我只有jsoncpp 0.5.0,该函数不可用.但这并没有什么不同.

I also removed the getFormattedErrorMessages() call, because I have only jsoncpp 0.5.0, in which that function is not available. That shouldn't make a difference though.

让我知道这是否对您有用.

Let me know if this works for you.

这篇关于如何为jsoncpp编写cmake模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆