在 C++ vidual studio 代码 Ubuntu 中包含 nlohmann/json

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

我是 C++ 的新手。我想包含来自那个库的 json https://github.com/nlohmann/json。我已经下载了,放在我的文件夹里

  • 我的CPP
    • main.cpp
    • json(nlohmann 文件夹)

在我的 main.cpp 中

#include <iostream>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
using namespace std;

int main(){
    json j2 = {
        {"pi", 3.141},
        {"happy", true},
    };

    cout << j2 << endl;

    return 0;
}

它说“致命错误:nlohmann/json.hpp:没有这样的文件或目录” 我不知道该怎么做才能将该文件夹包含在我的程序中......

谁能详细解释一下。

c++ shared-libraries
2个回答
0
投票

“nlohmann”文件夹应该在您正在构建的代码的包含路径上。

你能分享一下你是如何构建代码的吗?

此外,您应该使用“j2.dump()”将其发送到 cout。


0
投票

如果你的目录结构是:

MyCPP /
|-main.cpp
|-nlohmann /
  |-json.hpp

然后你应该可以使用

#include "nlohmann/json.hpp"

包含它

基本上区别是

<...>
搜索系统包含,而
"..."
搜索本地包含(如果你更详细的话会更复杂,但大多数你可以忽略它

编辑:当然,如果您的目录结构看起来像这样(如果您只是复制了 git 存储库,它可能会这样):

MyCPP /
|-main.cpp
|-nlohmann /
  |-include /
    |-nlohmann /
      |-json.hpp

然后你应该可以使用

#include "nlohmann/include/nlohmann/json.hpp"

包含它
© www.soinside.com 2019 - 2024. All rights reserved.