简体   繁体   English

我应该如何将其包含在我的静态库中?

[英]How should I include in my static library?

I'm creating a static library that I intend for other people to use. 我正在创建一个供他人使用的静态库。 This static library utilizes the Windows header, and in many of the functions it uses Windows definitions, such as DWORD, PDWORD, LPVOID, etc. I want these to be somewhat strict as to really define the functions in my library, reducing misuse. 该静态库利用Windows标头,并且在许多函数中都使用Windows定义,例如DWORD,PDWORD,LPVOID等。我希望它们在实际定义我的库时要严格一些,以减少滥用。 However, if I include the Windows header inside the header files of my static library, then someone who includes my library will automatically have the Windows header also included, and I don't believe this is the correct way of going about this process as many of the libraries I typically include in my other projects also require that I include the correct header files. 但是,如果我在静态库的头文件中包含Windows标头,则包含我的库的人也将自动包含Windows标头,而且我不认为这是进行此过程的正确方法,因为我通常包含在其他项目中的库中的一部分也要求我包含正确的头文件。 So how should I go about "requiring" that people who use my library also include the windows header, or at least have my library check to see if the windows header is already included? 因此,我应该如何“要求”使用我的库的人还包括Windows标头,或者至少让我的库检查是否已包含Windows标头? This applies to other headers too, just in general (ie if I had to include iostream, then I would want to check if the iostream header was already included). 总体上,这也适用于其他头文件(即,如果我必须包括iostream,那么我想检查iostream头文件是否已经包含在内)。

Essentially, should my static library take precedence over all of the user's includes, or adapt depending on what the user had already included? 本质上,我的静态库应该优先于所有用户的包含项,还是应根据用户已包含的内容进行调整? If the user includes my library, is it better to have them simply include my library's header and nothing else, or is there some way to make it so the user includes what they need in addition to my library? 如果用户包括我的图书馆,最好是让他们仅包括我的图书馆的标头,什么也不做,还是有某种方法可以使用户包括我的图书馆呢?

For some extra clarity: 为了更加清楚:

#include "MyLibrary.h"
BOOL WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpvReserved) {
    // Stuff
}

Or 要么

#include <Windows.h>
#include "MyLibrary.h"
BOOL WINAPI DllMain(HMODULE hModule, DWORD fdwReason, LPVOID lpvReserved) {
    // Stuff
}

It does not matter how many times you include Windows.h , it already encloses its content within a #ifndef - #endif construct. 包含Windows.h次数无关紧要,它已经将其内容包含在#ifndef #endif构造内。 If it was included once, some macro is already defined and the code in the header will not be included again. 如果只包含一次,则已经定义了一些宏,并且标头中的代码将不再包含。

On the other hand, if your library is static, it does not rely anymore on any external code, all symbols coming from Windows.h are effectively private to the library and not seen by anything else. 另一方面,如果您的库是静态的,则它不再依赖任何外部代码,所有来自Windows.h符号实际上都是该库专用的,其他任何对象都看不到。

EDIT : to clarify your last edit. 编辑 :澄清您的最后编辑。

The public API should be in the .h of your library. 公用API应该位于您的库的.h中。 If Windows.h is not part of the API you offer, you better put it in your .cc, so, second option. 如果Windows.h不是您提供的API的一部分,则最好将其放在.cc中,这是第二种选择。 If, on the contrary, some definitions from Windows.h are used in the definition of your public API, include it in your .h. 相反,如果公共API的定义中使用了Windows.h某些定义,则将其包含在.h中。

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

相关问题 我应该#include我的图书馆名称空间吗? - Should I #include in my library's namespace? 我应该使用单个头文件来包含所有静态库头文件吗? - Should I use a single header to include all static library headers? 我应该包含外部库的 src 代码还是从外部库构建的静态库? - Should I include src code of external library or a static library built from an external library? C++ - 我应该在我的代码中的什么地方包含 static_cast? - C++ - Where in my code should I include static_cast? 如果要测试,我应该将C ++代码编写为静态库还是动态库? - Should I write my C++ code as a static or dynamic library if I want to test it? 如何重构我的库以包含模板? - How can I refactor my library to include templates? 如何在我的C ++项目中包含activeX库? - How do I include an activeX library in my C++ project? 如何将DirectX库包含到我自己的静态库(独立库)中? - How to include DirectX libraries into my own static library (stand-alone lib)? 我可以将库包含为静态库,并可以在iOS项目中进行编译吗? - Can I include a library as a static library and compile it inside a iOS project? 我该如何在班上实现一个静态的字符串集合 - How should I implement a static collection of Strings in my class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM