linux 如何链接jsoncpp和cmake

pgvzfuti  于 2023-01-16  发布在  Linux
关注(0)|答案(4)|浏览(284)

我找不到将jsoncpp链接到可执行文件的方法。我尝试了很多方法,但都没有成功:

  • 链接jsoncpp_lib
  • 还有这里写的:
find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)
link_libraries(${JSONCPP_LIBRARIES})
add_executable(myprogram myprogram.cpp)
target_link_libraries(myprogram ${JSONCPP_LIBRARIES})

我想使用ubuntu自带的jsoncpp库,有人成功做到了吗?

$ ls /usr/lib/x86_64-linux-gnu/libjsoncpp.*

/usr/lib/x86_64-linux-gnu/libjsoncpp.a   /usr/lib/x86_64-linux-gnu/libjsoncpp.so.1
/usr/lib/x86_64-linux-gnu/libjsoncpp.so  /usr/lib/x86_64-linux-gnu/libjsoncpp.so.1.7.4

CMakeLists.txt

cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

project(jsoncpp_example)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -stdlib=libc++ -Wall -Werror")
set(CMAKE_EXE_LINKER_FLAGS "-lstdc++ -lpthread")

add_executable(parse main.cpp)
target_include_directories(parse PRIVATE include)
target_link_libraries(parse jsoncpp)

Parser.h

#ifndef MY_PARSER_H
#define MY_PARSER_H

#include <vector>
#include <fstream>
#include <jsoncpp/json/json.h>

class MyParser {
public:
    MyParser() = default;
    ~MyParser() = default;
    
    inline static 
    Json::Value parse(const char* inputFile) {
        Json::Value val;
        std::ifstream ifs(inputFile);
        Json::Reader reader;
        reader.parse(ifs, val);
        return val;
    }

};

#endif /* MY_PARSER_H */

main.cpp

#include <iostream>
#include "Parser.h"

int main(){
    Json::Value val = MyParser::parse("input.json");
    std::cout << val["name"] << std::endl;
}

Ubuntu 18.04,使用clan-8.0编译

CXX=clang-8 cmake ..
make -j

我得到的错误

Scanning dependencies of target parse
[ 50%] Building CXX object CMakeFiles/parse.dir/main.cpp.o
[100%] Linking CXX executable parse
CMakeFiles/parse.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x3e): undefined reference to `Json::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Json::Value const&)'
CMakeFiles/parse.dir/main.cpp.o: In function `MyParser::parse(char const*)':
main.cpp:(.text._ZN8MyParser5parseEPKc[_ZN8MyParser5parseEPKc]+0x349): undefined reference to `Json::Reader::parse(std::__1::basic_istream<char, std::__1::char_traits<char> >&, Json::Value&, bool)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/parse.dir/build.make:94: recipe for target 'parse' failed
make[2]: *** [parse] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/parse.dir/all' failed
make[1]: *** [CMakeFiles/parse.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
yvfmudvl

yvfmudvl1#

维基提到

get_target_property(JSON_INC_PATH jsoncpp_lib INTERFACE_INCLUDE_DIRECTORIES)
include_directories(${JSON_INC_PATH})

target_link_libraries(${PROJECT_NAME} jsoncpp_lib)

但我自己无法使其工作。如果您将jsoncpp.cmake文件安装在自动搜索的位置,则可以选择:

cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

project(jsoncpp_example)

find_package(PkgConfig REQUIRED)
pkg_check_modules(JSONCPP jsoncpp)

set(CMAKE_THREAD_PREFER_PTHREAD TRUE) # these are to use the prefered -pthread flag
set(THREADS_PREFER_PTHREAD_FLAG TRUE) # over the -lpthread option
find_package(Threads REQUIRED)

add_executable(parse main.cpp)
set_property(TARGET parse PROPERTY CXX_STANDARD 17)
target_compile_options(parse PRIVATE -Wall -Werror)

target_link_libraries(parse PRIVATE Threads::Threads ${JSONCPP_LIBRARIES})
qnakjoqk

qnakjoqk2#

在Linux上,这对我很有效
1.安装开发库。
sudo apt-获取安装libjsoncpp-dev
1.在cmakelist中。
软件包搜索模块(JSONCPP jsoncpp)
加到...
目标链接库(${JSONCPP库})

  1. c制造。
    1.制造
xuo3flqw

xuo3flqw3#

对于那些来到这个主题的人,像我一样,正在寻找一种方法来使用cmake将jsoncpp库包含在C++项目中,我通过将vcpkg存储库克隆到external文件夹中并通过vcpkg安装jsoncpp包来解决这个问题,正如官方的jsoncpp资源库所建议的那样,我已经更好地描述了这个问题的解决方案in this topic here,因为它在搜索术语中更通用。

bbuxkriu

bbuxkriu4#

使用cmake 3.22我可以让它工作:

find_package(jsoncpp REQUIRED)
...
target_link_libraries({app_name} ... jsoncpp_lib)

相关问题