我是 C++ 的新手。我想包含来自那个库的 json https://github.com/nlohmann/json。我已经下载了,放在我的文件夹里
在我的 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:没有这样的文件或目录” 我不知道该怎么做才能将该文件夹包含在我的程序中......
谁能详细解释一下。
“nlohmann”文件夹应该在您正在构建的代码的包含路径上。
你能分享一下你是如何构建代码的吗?
此外,您应该使用“j2.dump()”将其发送到 cout。
如果你的目录结构是:
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"
包含它