无法使用 cmake 链接 NLohmann json 库

问题描述 投票:0回答:1

我正在尝试通过 cmake 将 NLohmann json 库链接到我的 json 解释器。

我不断收到错误消息:

fatal error: 'nlohmann/json.hpp' file not found #include <nlohmann/json.hpp>

来自文件:

#include <nlohmann/json.hpp>
#include <fstream>
#include "InterpretJson.h"
#include "game.h"
using namespace std;
using json = nlohmann::json;

InterpretJson(string path){
    this->path = path;
    ifstream f(path);
    json jData = json::parse(f);
    f.close();
    this->data = jData;
}

Game interpret(Game& game){}

包含src/interpretJson.cpp和include/interpretJson.h的目录下的CMakeLists.txt:

find_package(nlohmann_json 3.2.0 REQUIRED)

add_library(interpreter
    src/interpretJson.cpp
)


target_include_directories(interpreter
    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}/src
        ${nlohmann_json_INCLUDE_DIR}
)

target_link_libraries(interpreter
    PRIVATE 
        ${nlohmann_json_LIBRARIES}
)

set_target_properties(interpreter
                    PROPERTIES
                    LINKER_LANGUAGE CXX
                    CXX_STANDARD 17
)

install(TARGETS interpreter
    ARCHIVE DESTINATION lib
)

我该如何解决这个问题?

编辑:这个问题只发生在 arm64 Mac M1 上,但它在 linux ubuntu VM 上运行良好。然而,vm 很慢,我仍然想知道如何让它在 Mac 上工作

cmake arm64 nlohmann-json find-package target-link-libraries
1个回答
0
投票

我的 CMakeLists.txt 中有这个

FetchContent_Declare(
        nlohmann
        GIT_REPOSITORY  "https://github.com/onavratil-monetplus/json"
        GIT_TAG         "master"
)

FetchContent_MakeAvailable( nlohmann )
target_include_directories ( main PUBLIC ${nlohmann_json_SOURCE_DIR}/include )

在 src 文件中 ...

#include "nlohmann/json.hpp"
© www.soinside.com 2019 - 2024. All rights reserved.