linux编译cpp文件命令,Jsoncpp Linux 下编译为 .a 文件

1 下载 jsoncpp 路径如下:

https://github.com/open-source-parsers/jsoncpp

2. 解压文件

unzip jsoncpp-master.zip

9ff2bac80c3ef8770457abc06f1b3377.png

3. 提取所需要的源码数据

include  目录  和  src/lib_json 目录

我这边如下效果:

users@(none):TestCode> tree json

json

├── include

│   └── json

│   ├── allocator.h

│   ├── assertions.h

│   ├── autolink.h

│   ├── config.h

│   ├── features.h

│   ├── forwards.h

│   ├── json.h

│   ├── reader.h

│   ├── value.h

│   ├── version.h

│   └── writer.h

└── src

├── json_reader.cpp

├── jsonTest.cpp

├── json_tool.h

├── json_value.cpp

├── json_valueiterator.inl

├── json_writer.cpp

└── version.h.in

3 directories, 18 files

在 src 目录加入自己的Makefile 文件 如下:

vim Makefile

CC=g++

AR=ar

SEPARATOR="~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"

jsonlib=libjsoncpp.a

SOURCES = json_reader.cpp json_value.cpp json_writer.cpp

INCLUDE = -I../include/

OBJS=$(patsubst %.cpp, %.o, $(SOURCES))

all: clean jsonlib

.cpp.o:

$(CC) ${OPTIMIZE} -o $@ -c $*.cpp $(INCLUDE)

@echo $(SEPARATOR);

jsonlib:$(OBJS)

$(AR) -r -v $(jsonlib) $(OBJS)

rm -rf *.o

@echo $(SEPARATOR);

clean:

rm -f ./$(jsonlib)

rm -f ./*.o

@echo $(SEPARATOR);

编译 便可生成 libjsoncpp.a 文件

4. 测试:

vim TestJson.cpp

#include

#include

#include "json/json.h"

using namespace std;

int main(int argc, char *argv[])

{

Json::Value root;

Json::Value arrayobj;

Json::Value item;

root["key"] = "value";

for (int i = 0; i < 10; i++) {

item["arrkey"] = i;

arrayobj.append(item);

}

root["array"] = arrayobj;

std::string out = root.toStyledString();

std::cout << out << std::endl;

return 0;

}

5 编译测试程序:

g++ TestJson.cpp -o TestJson -I../include/ -L. libjsoncpp.a

6. 运行如下:

{

"array" :

[

{

"arrkey" : 0

},

{

"arrkey" : 1

},

{

"arrkey" : 2

},

{

"arrkey" : 3

},

{

"arrkey" : 4

},

{

"arrkey" : 5

},

{

"arrkey" : 6

},

{

"arrkey" : 7

},

{

"arrkey" : 8

},

{

"arrkey" : 9

}

],

"key" : "value"

}

最后代码目录结构:

json

├── include

│   └── json

│   ├── allocator.h

│   ├── assertions.h

│   ├── autolink.h

│   ├── config.h

│   ├── features.h

│   ├── forwards.h

│   ├── json.h

│   ├── reader.h

│   ├── value.h

│   ├── version.h

│   └── writer.h

└── src

├── json_reader.cpp

├── jsonTest.cpp

├── json_tool.h

├── json_value.cpp

├── json_valueiterator.inl

├── json_writer.cpp

├── libjsoncpp.a

├── Makefile

├── TestJson

├── TestJson.cpp

└── version.h.in

3 directories, 22 files

7. 一个读 Json 测试 (TestJsonRead.cpp)

#include "json/json.h"

#include

#include

using namespace std;

string str = "{\"total\":1,\"toReturn\":[{\"createTime\":\"20080806114526000+0800\",\"sex\":\"先生\"},{\"createTime\":\"201706114526000+0800\",\"sex\":\"女士\"}],\"success\":false}";

int main(int argc, char *argv[])

{

Json::Reader reader;

Json::Value jv;

if (reader.parse(str, jv)) {

int total = jv["total"].asInt();

bool success = jv["success"].asBool();

std::cout << "total = " << total << std::endl

<< "success = " << success << std::endl;

size_t len = jv["toReturn"].size();

for (unsigned int i = 0; i < len; i++) {

std::string time = jv["toReturn"][i]["createTime"].asString();

std::string sex = jv["toReturn"][i]["sex"].asString();

std::cout << "id : " << i

<< " createTime : " << time

<< " sex : " << sex << std::endl;

}

} else {

std::cout << "Json::reader.parse error!" << std::endl;

}

return 0;

}

编译:

gcc TestJsonRead.cpp -o TestJsonRead -I../include libjsoncpp.a -lstdc++

运行:

total = 1

success = 0

id : 0 createTime : 20080806114526000+0800 sex : 先生

id : 1 createTime : 201706114526000+0800 sex : 女士

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值