0

我有一个带有以下代码的标头,该标头具有源自头文件 /usr/include/sys/param.h 中 CROUND 定义的神秘错误,该错误未包含在代码本身或通过命令行参数中:

#ifndef _PARSERTYPES_H_
#define _PARSERTYPES_H_    

#ifdef CROUND
#warning #undef CROUND applied, defined in /usr/include/sys/param.h  
#undef CROUND
#endif // CROUND
enum TokType
{
  FLOATING, INTEGER, STRING, SYMBOL,
    OCURLY, CCURLY, OROUND, CROUND, OSQUARE, CSQUARE, 
};    

#endif // _PARSERTYPES_H_

使用简化的 clang (c++) 或 gcc (g++) 编译器命令编译:

cd /path/to/build3/modules/device/examples/sweep && /usr/bin/g++   -DQT_OPENGL_LIB -DQT_DESIGNER_LIB -DQT_GUI_LIB -DQT_SQL_LIB -DQT_NETWORK_LIB -DQT_CORE_LIB -D__DC1394__ - -DQT_NO_DEBUG -O3 -DNDEBUG -I/path/to/modules//. -I/opt/local/include -I/opt/local/include/opencv -I/usr/local/include -I/opt/local/include/QtOpenGL -I/opt/local/include/QtDesigner -I/opt/local/include/QtGui -I/opt/local/include/QtSql -I/opt/local/include/QtNetwork -I/opt/local/include/QtCore -I/Library/Frameworks/FreeType.framework/Headers -F/Library/Frameworks -I/opt/local/include/glib-2.0 -I/opt/local/lib/glib-2.0/include -I/opt/local/include/gdk-pixbuf-2.0 -I/opt/local/include/gtk-2.0 -I/opt/local/lib/gtk-2.0/include -I/Library/Frameworks/cairo.framework/Headers -I/opt/local/include/pango-1.0 -I/opt/local/include/atk-1.0    -o CMakeFiles/Example.dir/Example.cpp.o -c /path/to/modules/Example.cpp

这会产生以下(clang)错误:

In file included from /path/to/parser/parser.h:69:
path/to/parser/parserTypes.h:58:26: error: expected '}'
        OCURLY, CCURLY, OROUND, CROUND, OSQUARE, CSQUARE, 
                                ^
/usr/include/sys/param.h:171:16: note: expanded from macro 'CROUND'
#define CROUND  (CBLOCK - 1)    /* Clist rounding. */
                ^
/path/to/parser/parserTypes.h:56:1: note: to match this '{'
{

我能够使用#undef CROUND 解决这个问题,但显然这是一个非常临时的解决方案。

什么可能是这个问题的根源和适当的解决方案?

4

1 回答 1

2

你可能包括一些系统头文件,对吧?这些通常包括一大堆其他标题......

编辑:至于解决方案...

a) 使用其他不冲突的名称

b)在相当新的编译器上,使用枚举类(除非您与宏冲突......这里似乎就是这种情况)

编辑2:一般来说,我不建议尝试通过廉价的技巧来“解决”其他标题,例如#defining包含保护宏或类似的东西。系统标头最好不要管。

于 2012-07-10T20:30:40.083 回答