我希望使用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问题,但不知道是否是这样。
发布于 2013-08-02 00:11:08
好的,我有一个在我的系统上编译很好的解决方案。查找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/installures.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,其中该函数不可用。不过,这不会有什么区别。
如果这对你有用的话请告诉我。
发布于 2015-02-17 19:58:19
jsoncpp
现在是cmake
。
cmake -DCMAKE_BUILD_TYPE=debug -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -G "Unix Makefiles" ../..
pkg-config
如果你有建议,在GitHub上打开一个问题。
https://stackoverflow.com/questions/18005880
复制相似问题