简体   繁体   English

C ++ MSVS dll标头#include问题

[英]C++ MSVS dll headers #include issues

I don't code with lib linking and dll for most of the time, recently when I do, i realized there could be something very wrong with the way i do my #include. 我大部分时间都不使用lib链接和dll进行编码,最近,当我这样做时,我意识到我#include的编写方法可能存在一些问题。

Is the following the correct/desirable way to do #include? 以下是执行#include的正确/理想方法吗?

suppose i have 3 projects (1) dll_A (2) dll_B (3) exe_1. 假设我有3个项目(1)dll_A(2)dll_B(3)exe_1。 dll_A depends on dll_B, exe_1 depends one dll_A. dll_A取决于dll_B,exe_1取决于一个dll_A。

the way i do my #include is as follows: 我执行#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

From here it can be seen that exe_1.h indirectly includes dll_b.h which is kind of bad for me, because I want exe_1.h to be independent of dll_b.h... yet, I am not sure if that is possible because how else can exe_1 link to dll_b? 从这里可以看出exe_1.h间接包含dll_b.h,这对我来说有点不好,因为我希望exe_1.h与dll_b.h无关...但是,我不确定这是否可能,因为exe_1还能如何链接到dll_b?

EDIT: Example dependency 编辑:依赖示例

// 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?

If dll_B is just an implementation detail of dll-A, then don't include [dll_B.h] from [dll_A.h], just include it from [dll_A.cpp]. 如果dll_B只是dll-A的实现细节,则不要包括[dll_A.h]中的[dll_B.h],而应包括[dll_A.cpp]中的。

Avoiding that header dependency may require a little redesign. 避免头依赖项可能需要重新设计。

Eg you may want to think about PIMPL idiom for [dll_A]. 例如,您可能想考虑[dll_A]的PIMPL习惯用法

More details are impossible to state without knowing more details... :-) 如果不知道更多细节,就无法陈述更多细节... :-)

Cheers & hth., 干杯,……

PS: Linking has nothing to do with header files, except that with some Windows compilers (notably MSVC) a header file can include a #pragma that causes automatic linking with correct library. PS:链接与头文件无关,除了在某些Windows编译器(尤其是MSVC)中,头文件可以包含#pragma ,后者会导致与正确的库自动链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM