【问题标题】:C++ MSVS dll headers #include issuesC++ MSVS dll 头文件#include 问题
【发布时间】:2010-10-23 02:36:51
【问题描述】:

我大部分时间都不使用 lib 链接和 dll 进行编码,最近当我这样做时,我意识到我执行 #include 的方式可能有一些非常错误的地方。

以下是执行#include 的正确/理想方式吗?

假设我有 3 个项目 (1) dll_A (2) dll_B (3) exe_1。 dll_A依赖dll_B,exe_1依赖一个dll_A。

我的#include 方法如下:

dll_B.h ----> no dependency
dll_B.cpp -----> #include dll_B.h
dll_A.h -------> #include dll_B.h
dll_A.cpp -------> #include dll_A.h
exe_1.h --------> #include dll_A.h

从这里可以看出 exe_1.h 间接包含 dll_b.h 这对我来说有点不好,因为我希望 exe_1.h 独立于 dll_b.h...但是,我不确定是否是可能的,因为 exe_1 还能如何链接到 dll_b?

编辑:示例依赖

// dll_B.h
struct dataB{};

// dll_A.h
#include dll_B.h
dataB* A_function_ptr(); // (any difference for implementing PIMPL?)
dataB& A_function_ref();
dataB A_function_copy();

// exe_1.cpp
#include dll_A.h
// ... it seems naturally #include-sion of dll_B.h is necessary? Can it be avoided?

【问题讨论】:

    标签: c++ dll include header


    【解决方案1】:

    如果 dll_B 只是 dll-A 的一个实现细节,那么不要从 [dll_A.h] 中包含 [dll_B.h],只需从 [dll_A.cpp] 中包含它。

    避免头文件依赖可能需要重新设计。

    例如您可能想为 [dll_A] 考虑 PIMPL idiom

    如果不知道更多细节,就不可能说出更多细节...... :-)

    干杯,

    PS:链接与头文件无关,除了在某些 Windows 编译器(特别是 MSVC)中,头文件可以包含 #pragma,这会导致自动链接到正确的库。

    【讨论】:

    • 感谢 Alf,当我说链接时,我实际上还意味着要了解 dll_A 中使用的 dll_B 中的声明。假设 dll_B 有一个 struct B_DATA 是 dll_A 中 A_function 的返回类型,那么这也是一个糟糕的设计吗?
    猜你喜欢
    • 2022-10-12
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2012-05-08
    相关资源
    最近更新 更多