开发者

C++ MSVS dll headers #include issues

开发者 https://www.devze.com 2023-01-21 05:41 出处:网络
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.

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.

Is the following the correct/desirable way to do #include?

suppose i have 3 projects (1) dll_A (2) dll_B (3) exe_1. dll_A depends on dll_B, exe_1 depends one dll_A.

the way i do my #include is as follows:

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?

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].

Avoiding that header dependency may require a little redesign.

E.g. you may want to think about PIMPL idiom for [dll_A].

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号