编译器抱怨即使有警卫在场也重新定义
Compiler complains about redefinition even with guards present
我正在尝试为OpenGL应用程序设置一些日志记录函数。GLFW 提供了注册回调函数的选项,每当发生错误时都会调用该函数,但由于它是一个 C 库,因此它要求该函数以 C 样式编写,即在类之外。因此,我将日志记录函数放入文件log.h
中定义的命名空间中。
#ifndef LOG_H
#define LOG_H
#include <fstream>
namespace gllog{
#define GL_LOG_FILE "gl.log"
bool restart_gl_log(){
//...
}
bool gl_log (const char* message, const char* filename, int line){
//...
}
void glfw_error_callback (int error, const char* description){
//...
}
};
#endif
即使我添加了包含保护,每当我从两个不同的文件包含此文件时,我也会收到如下错误:
CMakeFiles/gl4tuts.dir/extended_initialisation/ExtendedInitialisation.cpp.o: In function `gllog::glfw_error_callback(int, char const*)':
ExtendedInitialisation.cpp:(.text+0x340): multiple definition of `gllog::glfw_error_callback(int, char const*)'
CMakeFiles/gl4tuts.dir/hello_triangle/HelloTriangle.cpp.o:HelloTriangle.cpp:(.text+0xa10): first defined here
我正在使用CMake构建。
可能是什么原因?没有类可能有关吗?
包含保护可防止您在编译同一.cpp
文件时多次包含相同的头文件。它们不会阻止您在编译不同的.cpp
文件时多次包含相同的头文件 - 实际上,它们通常用于使同一类在不同的编译单元中可用。
顺便说一下,编译器没有抱怨,链接器是。
相关文章:
- 定义C++新的环境变量并在 bat 文件中使用它
- 是否可以使用单个定义定义函数的常量和常规版本?(使用模板,自动,decltype等)
- 从使用概念定义的函数返回新对象
- 如何在C++中为 if 和 else 语句定义新行为
- 如何从 C++ 中的现有模板函数定义新函数
- 自定义分配器,包括放置新案例
- 正在通过const-ref未定义的行为捕获新构造的对象
- 如何在CPropertySheet中定义新颜色
- 谁定义新操作员
- 未定义的符号'fixed_address_empty_string':带有protobuf的新张量流运算
- 新的C ,未定义的参考
- boost :: fibonacci_heap:带有比较器重新定义圆形定义错误的嵌套定义
- 宏,用于定义带有添加前缀的新宏
- 取消脱壳:使用模板在多行类型定义中添加一个尖括号后的新行
- C 通过Typedef定义新类型
- 通过在此指针上放置新位置重新初始化对象时未定义的行为
- 在 ss.clear() 之后使用 ss.str( " ") 用于新定义的字符串流
- Eclipse CDT将方法定义定义到标头文件
- 是标头文件中使用“新”定义的成员指针泄漏
- LEX 程序中的未定义定义