有关 DLL 中包含的说明
Clarification on Includes in DLLs
我最近遇到了一些违背我对包含的理解的东西。
我正在创建一个 dll 来保存我正在开发的个人游戏引擎的基本坐标对象(为了好玩(。
主 dll 头文件。
#pragma once
#include "sclapi.h"
#include "vector.h"
#include "point.h"
// Other includes and stuff.
// Unimportant for this demonstration.
斯克拉皮·
#pragma once
#ifdef SCL_EXPORTS
#define SCL_API __declspec(dllexport)
#else
#define SCL_API __declspec(dllimport)
#endif
矢量.h
#pragma once
// No includes
/*EDIT*/struct Point;
struct SCL_API Vector {
float x, y;
// Other stuff
explicit operator Point() const;
};
点.h
#pragma once
// No includes
struct SCL_API Point {
int x, y;
// Other stuff
explicit operator Vector() const;
};
我的代码工作得很好;但据我所知,它不应该。头文件应该只知道其中声明的内容(包括作为粘贴代码的速记(。这些对象都不会在另一个对象的头文件中声明。[编辑] point.h 应该不知道矢量结构。更重要的是,两人甚至都了解SCL_API宏。如果我注释掉主头文件中的各个包含,我会收到预期的编译器错误。我错过了什么?
编辑:
经过进一步的测试,我发现后面对象的声明需要在第一个头文件'vector.h'中;但之后,它们不需要在任何其他头文件中再次声明。此外,在主头文件中声明类不起作用。点前向声明必须位于 vector.h 文件中。
当您#include
文件时,它会自动"复制粘贴"到您的源中。
因为您在包含point.h
之前包含了vector.h
,所以Point
类将看到它。
但是,依赖此行为不是一个好主意,因为包含的顺序可能会更改,因此它将不再起作用,因此您应该#include "vector.h"
point.h
。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 编译包含字符串的代码时遇到问题
- c++库的公共头文件中应该包含什么
- 将包含C样式数组的对象初始化为成员变量(C++)
- 是否需要删除包含对象的"pair"?
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何将包含epoch时间的十六进制字符串转换为time_t
- 使用mongocxx驱动程序时包含头文件问题
- 如何在h文件中包含.o对象文件
- 在混合代码库中将C转换为C++时出现许多包含错误
- VS2017,C++包含目录与附加包含目录,子文件夹包含失败-但为什么
- cmath抛出错误C2062、C2059、C2143和C2447.cmath包含在矢量文件中
- 为什么您需要C++头文件的包含保护
- 无法在UE4中包含BP类到CPP类
- 有关 DLL 中包含的说明
- 组装的C 似乎包含多余的说明
- 如何在C 中编写一个程序,以说明不知道它正在阅读的数据文本文件中包含多少行
- 使用Doxygen时,请勿在详细说明中包含简要说明
- 包含const_cast的代码片段的说明