Zlo*_*loj 7 c c++ lint header-files
简短的介绍:
Header.h具有#include <stdbool.h>其具有在C为_Bool宏.
file.cpp包括Header.h,但由于file.cpp是C++ - 它有bool作为本机类型.现在lint抱怨一系列的事情(重新声明,不存在的方法等).有没有办法防止包含<stdbool.h>在file.cpp没有接触Header.h?
如果我对问题的描述看起来很荒谬 - 请向我扔西红柿:)否则,谢谢你的帮助.
编辑:现在再考虑一下:了解编译和链接的基本概念我应该意识到"排除"下游文件/标题中的某些标题听起来很有趣,不应该没有cludges.但是,谢谢你的帮助.另一个小砖块让我对此有所了解.
Jon*_*ely 11
您可以创建自己的stdbool.h并将其放在包含路径中,以便在系统之前找到它.这是技术上未定义的行为,但你已经破了,<stdbool.h>所以这是解决这个问题的一种方法.您自己的版本可能为空(如果它只包含在C++文件中),或者如果您无法阻止它也被C文件使用,那么您可以执行以下操作:
#if __cplusplus
# define __bool_true_false_are_defined 1
#elif defined(__GNUC__)
// include the real stdbool.h using the GNU #include_next extension
# include_next <stdbool.h>
#else
// define the C macros ourselves
# define __bool_true_false_are_defined 1
# define bool _Bool
# define true 1
# define false 0
#endif
Run Code Online (Sandbox Code Playgroud)
一个更清洁的解决方案是在此file.cpp 之前执行此操作,包括Header.h:
#include <stdbool.h>
// Undo the effects of the broken <stdbool.h> that is not C++ compatible
#undef true
#undef false
#undef bool
#include "Header.h"
Run Code Online (Sandbox Code Playgroud)
现在当Header.h包含<stdbool.h>它时它将没有任何效果,因为它已被包含在内.这种方式在技术上是无效的(见下面的评论),但实际上几乎可以肯定地工作.
它需要在包含的每个文件中完成Header.h,因此您可以将其包装在新的标头中并使用它而不是Header.h例如CleanHeader.h包含:
#ifndef CLEAN_HEADER_H
#define CLEAN_HEADER_H
// use this instead of Header.h to work around a broken <stdbool.h>
# include <stdbool.h>
# ifdef __cplusplus
// Undo the effects of the broken <stdbool.h> that is not C++ compatible
# undef true
# undef false
# undef bool
#e ndif
# include "Header.h"
#endif
Run Code Online (Sandbox Code Playgroud)