268,798
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
我已经生成了静态文件,如图所示
我建立了一个叫做test的项目,项目目录为
test/
├── src/
│ ├── run.cpp
├── lib/
│ ├── libjsoncpp.a
└── build
其中run.cpp的代码为
#include <json/json.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
//std::string strValue = "{\"name\":\"json\",\"array\":[{\"cpp\":\"jsoncpp\"},{\"java\":\"jsoninjava\"},{\"php\":\"support\"}]}";
// 构建json数组
Json::Value array;
Json::Value root;
Json::Value person;
Json::FastWriter writer;
person["name"] = "allen";
person["age"] = 10;
person["sex"] = "male";
root.append(person);
person["name"] = "keiv";
person["age"] = 20;
person["sex"] = "female";
root.append(person);
person["name"] = "lihua";
person["age"] = 10;
person["sex"] = "female";
root.append(person);
// 添加数组格式
// array["array"].append(root);
// 子节点挂到根节点上
array["array"] = Json::Value(root);
string data = writer.write(array);
cout<<data<<endl;
// cout<<array.toStyledString()<<endl;
// 解析Json字符串
string strValue = array.toStyledString(); // json对象转变为json字符串
cout << strValue << endl;
Json::Reader reader;
Json::Value value;
if (reader.parse(strValue, value)) // json字符串转为json对象
{
for (unsigned int i = 0; i < value["array"].size(); i++)
{
string name = value["array"][i]["name"].asString();
int age = value["array"][i]["age"].asInt();
string sex = value["array"][i]["sex"].asString();
cout << name << " " << age << " " << sex << endl;
}
}
return 0;
}
然后我就编写了一个makefile文件。其内容为
CXX = g++
CXXFLAGS = -std=c++11 -Wall
LIBS = -L ./lib -ljsoncpp
INCLUDE = -I./include
SRCDIR = src
OBJDIR = build
SOURCES := $(shell find $(SRCDIR) -name *.cpp)
OBJECTS := $(SOURCES:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
DEPENDS := $(OBJECTS:.o=.d)
EXECUTABLE = test
.PHONY: all clean
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CXX) $(CXXFLAGS) $(LIBS) $(INCLUDE) $^ -o $@
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
@mkdir -p $(@D)
$(CXX) $(CXXFLAGS) $(INCLUDE) -MMD -c $< -o $@
clean:
$(RM) -r $(OBJDIR) $(EXECUTABLE)
但是在make文件时报错:
我看它似乎没有链接进来,有没有大佬
这是什么问题
将json/json.h文件放入include中去
目录结构:
include/json/json.h
另外从编译结果看-I./include在编译命令上也没有