您的位置:首页 > Web前端 > JavaScript

解决fatal error: bsoncxx/json.hpp: 没有那个文件或目录

2017-07-06 00:52 369 查看
今晚尝试封装mongodb api工具类,刚写了开头,进行测试,发现如下问题:
编译过程出现fatal error: bsoncxx/json.hpp: 没有那个文件或目录

看到提示,编译器应该是还没有完成预编译就已经出错,
随即猜测应该是头文件有问题,然后查编译器默认搜索路径,发现头文件存在且没有问题.

然后,回到之前写过的包含该头文件的代码,手写编译代码进行测试,发现编译通过.

由此,应该证实不是头文件的问题,可是为什么编译器会报错提示:没有那个文件或目录呢?

先上代码:

mongotool.h

  1 #ifndef _MONGOTOOL_H_

  2 #define _MONGOTOOL_H_

  3 

  4 #include <cstdint>

  5 #include <iostream>

  6 #include <bsoncxx/json.hpp>

  7 #include <mongocxx/client.hpp>

  8 #include <mongocxx/stdx.hpp>

  9 #include <string>

 10 #include <vector>

 11 #include <mongocxx/uri.hpp>

 12 #include <mongocxx/instance.hpp>

 13 #include <mongocxx/result/insert_one.hpp>

 14 

 15 using bsoncxx::builder::stream::document;

 16 using bsoncxx::builder::stream::close_document;

 17 using bsoncxx::builder::stream::open_document;

 18 using bsoncxx::builder::stream::open_array;

 19 using bsoncxx::builder::stream::close_array;

 20 using bsoncxx::builder::stream::finalize;

 21 using namespace std;

 22 

 23 class mongotool

 24 {

 25     private:

 26         mongotool();

 27         mongotool(mongotool&);

 28         ~mongotool();

 29     public:

 30         static mongotool* getInstance();

 31         int insert(int user_file_id,char* dst_path,char* md5);

 32     private:

 33         static mongotool *tool;

 34         mongocxx::instance instance{};

 35         mongocxx::client client{mongocxx::uri{}};

 36         mongocxx::database db;

 37         mongocxx::collection collection;

 38 };
#endif

mongotool.cpp
#include "mongotool.h"

  2 

  3 mongotool* mongotool::tool = new mongotool;

  4 

  5 mongotool::mongotool()

  6 {

  7     db = client["mynetdisk"];

  8     collection = db["file_system"];

  9 }

 10 

 11 mongotool::mongotool(mongotool&)

 12 {

 13 

 14 }

 15 mongotool::~mongotool()

 16 {

 17 

 18 }

 19 mongotool* mongotool::getInstance()

 20 {

 21     return tool;

 22 }

 23 

 24 int mongotool::insert(int user_file_id,char* dst_path,char* md5)

 25 {

 26     bsoncxx::document::value doc_value = document{} << "user_file_id"<<user_file_id<<"dst_path"<    <dst_path<<"md5"<<md5<<finalize;

 27     bsoncxx::stdx::optional<mongocxx::result::insert_one> result = collection.insert_one((bsoncx    x::document::value&&)doc_value);

 28     if(result)

 29     {

 30         return 0;

 31     }

 32     else

 33     {

 34         return -1;

 35     }

 36 

 37 }

 38 

 39 int main()

 40 {

 41     mongotool *mongo = mongotool::getInstance();

 42     if(mongo->insert(2,(char*)"/root/a.txt",(char*)"fsjdljjflsj"))

 43     {

 44         cout<<"mongo insert error"<<endl;

 45     }

 46     return 0;

 47 }

 48 

makefile

  1 all:out

  2 

  3 #out:mysqltool.cpp

  4 #   g++ mysqltool.cpp -o mysqltool.out -I/usr/include/mysql -lmysqlclient

  5 out:mongotool.cpp

  6     g++ --std=c++11 mongotool.cpp -o mongotool.out $(pkg-config --cflags --libs libmongocxx)

  7 #   g++ --std=c++11 mongotool.cpp -o mongotool.out 

  8       -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 \

  9             -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 \

 10               -L/usr/local/lib -lmongocxx -lbsoncxx

~                                                                                                   

~              

根据mongodb api的官方文档,拷贝了两种编译方式,写入makefile

用makefile中第6行或者第7-10行,分别对文件进行编译,提示信息相同,均为:
fatal error: bsoncxx/json.hpp: 没有那个文件或目录

查询百度,并查看自己的代码,花了一个小时,居然没有发现问题在哪.
直到有一次,在命令行手动输入第六行代码:
g++ --std=c++11 mongotool.cpp -o mongotool.out $(pkg-config --cflags --libs libmongocxx)
居然编译通过了.

因为是官方文档拷贝的编译方式,并且两种编译方式提示同样问题,
因此一直没有怀疑过是makefile过程中出现了问题...
其实仔细看makefile的代码,是可以发现问题的

第六行代码g++ --std=c++11 mongotool.cpp -o mongotool.out $(pkg-config --cflags --libs libmongocxx)
到$取变量时,编译器要求的是系统变量,但是在makefile中取不到,因此编译提示没有头文件

第七行代码g++ --std=c++11 mongotool.cpp -o mongotool.out 
行尾少了\,因此shell执行完第七行语句,后面的语句不再执行
因此同样提示缺少头文件

由此得出,即使是官方文档copy出来的语句,也不能完全相信

最终解决方案:
(1)若要用makefile,makefile文档内使用一下编译语句:
g++ --std=c++11 mongotool.cpp -o mongotool.out
\

        -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/libmongoc-1.0 \

        -I/usr/local/include/bsoncxx/v_noabi -I/usr/local/include/libbson-1.0 \
-L/usr/local/lib -lmongocxx -lbsoncxx

(2)以下语句只在shell界面下使用
g++ mysqltool.cpp -o mysqltool.out -I/usr/include/mysql -lmysqlclient
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐