简体   繁体   English

Xcode和Clang神秘地包含了我没有指定的标题

[英]Xcode and Clang mysteriously include headers I don't specify

I have a header with the following code which has mysterious errors stemming from the definition of CROUND in the header file /usr/include/sys/param.h which is not included either in the code itself or via the command line parameters: 我有一个带有以下代码的标题,其中包含来自头文件/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_

Compiled with the simplified clang (c++) or gcc (g++) compiler command: 使用简化的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

That produces the following (clang) error: 这会产生以下(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 '{'
{

I was able to work around the problem with #undef CROUND, but obviously this is a very temporary solution. 我能够使用#undef CROUND来解决这个问题,但显然这是一个非常临时的解决方案。

What could be the source of and proper solution to this problem? 什么可能是解决这个问题的根源?

You are probably including SOME system header file, right? 你可能包含了一些系统头文件,对吗? These usually include a whole pile of other headers... 这些通常包括一堆其他标题......

EDIT: as for a solution... 编辑:至于解决方案......

a) use other names that's don't conflict a)使用其他不冲突的名称

b) on reasonably recent compilers, use enum class (unless you are conflicting with a macro... which appears to be the case here) b)在合理的最新编译器上,使用枚举类(除非你与宏相矛盾......这似乎是这里的情况)

EDIT 2: in general, I would NOT recommend trying to "work around" that other header by cheap tricks such as #defining the include guard macro or similar stuff. 编辑2:一般情况下,我不建议尝试通过廉价技巧“解决”其他标题,例如#defining包含保护宏或类似的东西。 System headers are best left alone. 系统标题最好不要管它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM