如何在Linux上的c++程序中使用共享库(本例中是JsonCpp) ?

时间:2021-10-13 07:00:03

I'm a new-ish C++ programmer, and I'm doing my first program on my own using C++. I decided I would like to use JSON to store some of the data I'm going to be using, and I've found a library to handle JSON, JsonCpp.

我是一个新的c++程序员,我正在用c++编写我的第一个程序。我决定使用JSON存储将要使用的一些数据,并找到了一个处理JSON的库JsonCpp。

I've installed the library using my Linux system's package manager, and in my C++ code, I've used in my source code file

我使用Linux系统的包管理器安装了这个库,在c++代码中,我在源代码文件中使用了这个库

#include <json>

and compiled it using g++ and it's -ljson and -L/usr/lib options (libjson.so is located in /usr/lib).

使用g++编译它是-ljson和-L/usr/lib选项(libjson)。so位于/usr/lib中)。

However, the first usage of Json::Value, an object provided by the library, gives a compilation error of "Json has not declared". I'm sure my mistake is something simple, so could someone explain what I'm doing wrong? None of the books I had mention how to use shared libraries, so I've had to google to find this much.

但是,第一次使用Json::Value时,库提供的一个对象会出现“Json还没有声明”的编译错误。我确信我的错误很简单,有人能解释我做错了什么吗?我没有提到如何使用共享库的书,所以我不得不通过谷歌来找到这么多。

EDIT: g++ with the -E option gives this error:

编辑:使用-E选项的g++会出现以下错误:

json: no such file or directory.

json:没有这样的文件或目录。

3 个解决方案

#1


4  

I checked the file list of JsonCPP:

我查看了JsonCPP的文件列表:

include/json/autolink.h [code]  
include/json/config.h [code]    
include/json/features.h [code]  
include/json/forwards.h [code]  
include/json/json.h [code]  
include/json/reader.h [code]    
include/json/value.h [code] 
include/json/writer.h [code]

Try #include <json/json.h> if the headers are installed in /usr/include. If they're installed somewhere else, mention this path with -I

尝试# include < json / json。h>如果标头安装在/usr/ include上。如果它们被安装在其他地方,用-I表示这条路径

g++ -I/my/lib/include -L/my/lib/lib -lmylib mysource.cpp

#2


0  

Did you also tell g++ where to find the header files via -I - this would be my guess at the problem.

你有没有告诉g++在哪里可以通过- i -这是我对问题的猜测。

#3


0  

That error pretty nearly certainly implies that you do not have the #include in the actual source file that gets the error.

这个错误几乎可以肯定地说,您在获得错误的实际源文件中没有#include。

use

使用

 g++ -E [whatever other options]

to see the cpp output would be one way to check and see what you've actually included.

要查看cpp输出,可以检查并查看实际包含的内容。

It might help if you actually paste the error message; your comment suggests an error on the #include, but your question suggests something else.

如果您真的粘贴了错误消息,这可能会有所帮助;你的评论暗示了#include的错误,但是你的问题暗示了其他的东西。

#1


4  

I checked the file list of JsonCPP:

我查看了JsonCPP的文件列表:

include/json/autolink.h [code]  
include/json/config.h [code]    
include/json/features.h [code]  
include/json/forwards.h [code]  
include/json/json.h [code]  
include/json/reader.h [code]    
include/json/value.h [code] 
include/json/writer.h [code]

Try #include <json/json.h> if the headers are installed in /usr/include. If they're installed somewhere else, mention this path with -I

尝试# include < json / json。h>如果标头安装在/usr/ include上。如果它们被安装在其他地方,用-I表示这条路径

g++ -I/my/lib/include -L/my/lib/lib -lmylib mysource.cpp

#2


0  

Did you also tell g++ where to find the header files via -I - this would be my guess at the problem.

你有没有告诉g++在哪里可以通过- i -这是我对问题的猜测。

#3


0  

That error pretty nearly certainly implies that you do not have the #include in the actual source file that gets the error.

这个错误几乎可以肯定地说,您在获得错误的实际源文件中没有#include。

use

使用

 g++ -E [whatever other options]

to see the cpp output would be one way to check and see what you've actually included.

要查看cpp输出,可以检查并查看实际包含的内容。

It might help if you actually paste the error message; your comment suggests an error on the #include, but your question suggests something else.

如果您真的粘贴了错误消息,这可能会有所帮助;你的评论暗示了#include的错误,但是你的问题暗示了其他的东西。