问题描述
项目本身可以编译成功,由于使用了rapidjson而引入了如下三个头文件
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
此时编译后出现以下错误(只挑选了关键的)
bc_out/public/protobuf-json/output/include/rapidjson/reader.h: At global scope:
bc_out/public/protobuf-json/output/include/rapidjson/reader.h:84: error: expected unqualified-id before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/reader.h:84: error: expected `)' before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/reader.h: In member function `void rapidjson::GenericReader<SourceEncoding,TargetEncoding,Allocator>::ParseNumber(InputStream&,Handler&)':
bc_out/public/protobuf-json/output/include/rapidjson/reader.h:632: error: expected unqualified-id before '(' token
bc_out/public/protobuf-json/output/include/rapidjson/reader.h:632: error: expected primary-expression before "unsigned"
In file included from baidu/xfire/xcore2/plugins/material_search/src/material_search_proc_query_db.cpp:15:
bc_out/public/protobuf-json/output/include/rapidjson/document.h: In member function `const rapidjson::GenericValue<Encoding,Allocator>& rapidjson::GenericValue<Encoding,Allocator>::Accept(Handler&) const':
bc_out/public/protobuf-json/output/include/rapidjson/document.h:549: error: expected unqualified-id before '(' token
bc_out/public/protobuf-json/output/include/rapidjson/document.h:549: error: expected primary-expression before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/document.h: At global scope:
bc_out/public/protobuf-json/output/include/rapidjson/document.h:791: error: expected unqualified-id before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/document.h:791: error: expected `)' before "unsigned"
In file included from bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:4,from baidu/xfire/xcore2/plugins/material_search/src/material_search_proc_query_db.cpp:17:
bc_out/public/protobuf-json/output/include/rapidjson/writer.h:45: error: expected unqualified-id before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/writer.h:45: error: expected `)' before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/writer.h:45: error: abstract declarator `rapidjson::Writer<OutputStream,SourceEncoding,Allocator>&' used as declaration
bc_out/public/protobuf-json/output/include/rapidjson/writer.h:45: error: expected `;' before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/writer.h:46: error: expected `;' before "Writer"
In file included from baidu/xfire/xcore2/plugins/material_search/src/material_search_proc_query_db.cpp:17:
bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:46: error: expected unqualified-id before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:46: error: expected `)' before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:46: error: abstract declarator `rapidjson::PrettyWriter<OutputStream,Allocator>&' used as declaration
bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:46: error: expected `;' before "unsigned"
bc_out/public/protobuf-json/output/include/rapidjson/prettywriter.h:47: error: expected `;' before "PrettyWriter"
直接的意思是说reader.h头文件出错了,但显然rapid库本身没有错。
分析解决
reader.h出问题的代码84行如下:
void Uint(unsigned i);
定义了一个Uint的函数,根据经验有可能是某个地方也定义了相同的函数,且命名空间相同导致冲突,经过查找并没有,这时google到github上有人提出类似问题。可能是宏和Uint冲突了,于是做如下修改
#ifdef Uint
#undef Uint
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#endif
此时编译可以通过,但是毕竟我们强行undef了一个宏的定义,于是在依赖的文件中搜索Uint宏的定义
find . -name "*.h" | xargs grep "#define Uint"
./bc_out/lib2-64/ullib/output/include/ul_def.h:#define Uint(inp) (unsigned int)(inp)
./lib2-64/ullib/include/ul_def.h:#define Uint(inp) (unsigned int)(inp)
找到了该宏的定义,最后把其补充上,防止某些地方出现问题,最终解决后引用修改如下
#ifdef Uint
#undef Uint
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/prettywriter.h"
#define Uint(inp) (unsigned int)(inp)
#endif
此时编译通过,并且可以顺利执行。
BTW rapid json的确比Boost快,8w行的数据,boost用了近30s,rapid只用了不到2s。
补充
了解到了宏是全局作用的,所以就会和其他函数、变量冲突,对于函数,还有个解决方案是,把函数名用括号括起来,比如
(std::min)(x,y);
此时min就不会被其他的定义为min的宏覆盖。 当然,这只对自己的代码有用,对于第三方库就不能这个改了。