我花了很长时间试图找出以下不能编译的原因:
enum IPC_RC {OK, EOF, ERROR, NEW };
Run Code Online (Sandbox Code Playgroud)
错误消息只表示它不期望看到一个开括号.直到我尝试在我学到的更现代的编译器上编译它:
/usr/include/stdio.h:201:13: note: expanded from macro 'EOF'
#define EOF (-1)
Run Code Online (Sandbox Code Playgroud)
所以我终于被一个宏烧了!:)
我的代码没有#include <stdio.h>(我没有包含.h后缀的任何内容),但显然我包含的内容导致包含<stdio.h>.是否有任何方法(命名空间?)来保护自己,而不是追溯它包含在哪里?
我不知道您所描述的问题的令人满意的解决方案,但我只是想分享一种处理这种情况的方法。你时不时地(必须)使用一些特别令人讨厌的标题,它重新定义了英语的很大一部分。Python.h我想到了X11 标头。我最终所做的 - 并且效果很好 - 是(通常在我注意到破损之后)我将第 3 方标头包装在我自己的标头中并处理那里的丑陋。
例如,在使用 Ruby 解释器的项目中,我通常不包含ruby.h目录,而是包含一个ourruby.h如下所示的文件:
#ifndef RUBY_OURRUBY_H
#define RUBY_OURRUBY_H
// In Ruby 1.9.1, win32.h includes window.h and then redefines some macros
// which causes warnings. We don't care about those (we cannot fix them).
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4005)
#endif
#include <ruby.h>
#ifdef _MSC_VER
# pragma warning(pop)
#endif
// In Ruby 1.8.7.330, win32.h defines various macros which break other code
#ifdef read
# undef read
#endif
#ifdef close
# undef close
#endif
#ifdef unlink
# undef unlink
#endif
// ...
#endif // !defined(RUBY_OURRUBY_H)
Run Code Online (Sandbox Code Playgroud)
这样,我就不必记住某些标头不完全是命名空间干净的事实。