【问题标题】:Clarification on Includes in DLLs关于包含在 DLL 中的说明
【发布时间】:2018-05-23 17:17:23
【问题描述】:

我最近遇到了一些违背我对包含的理解的事情。

我正在创建一个 dll 来保存我正在开发的个人游戏引擎的基本坐标对象(为了好玩)。

主 dll 头文件。

#pragma once

#include "sclapi.h"
#include "vector.h"
#include "point.h"
// Other includes and stuff.
// Unimportant for this demonstration.

sclapi.h

#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;
};

point.h

#pragma once

// No includes

struct SCL_API Point {
    int x, y;

    // Other stuff

    explicit operator Vector() const;
};

我的代码运行良好;但据我了解,它不应该。头文件应该只知道其中声明的内容(包括粘贴代码的简写)。这些对象都没有在其他的头文件中声明。 [已编辑] point.h 应该不了解 Vector 结构。更重要的是,他们甚至都知道 SCL_API 宏。如果我注释掉主头文件中的单个包含,我会得到预期的编译器错误。我错过了什么?

编辑:

经过进一步测试,我发现后面的对象的声明需要在第一个头文件'vector.h'中;但之后,它们不需要在任何其他头文件中再次声明。此外,在主头文件中声明类也不起作用。 Point forward 声明必须在vector.h 文件中。

【问题讨论】:

  • 使用前向声明没有魔法发生。这按预期工作。

标签: c++ visual-c++ dll include


【解决方案1】:

当您#include 一个文件时,它会自动“复制粘贴”到您的源中。
因为您在包含point.h 之前包含了vector.h,所以Point 类将看到它。

但是,依赖这种行为并不是一个好主意,因为包含的顺序可能会改变,因此它不再起作用,因此您应该在 point.h 中使用 #include "vector.h"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2019-08-29
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多