当使用dll库,是否有必要包括用于构建dll的所有标题? [英] When using a dll library, is it necessary to include all of the headers used to build the dll?

查看:146
本文介绍了当使用dll库,是否有必要包括用于构建dll的所有标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从C ++源代码构建了一个共享库( .dll )。

I have built a shared library (.dll) from a C++ source code.

想要在另一个程序中使用这个库,我需要包括最初用于构建库的所有头文件,或者只包括属于我的(新)程序中使用的函数的头足够?库中有许多函数在我的新程序中不是直接使用,但是不用说,所有这些函数都由程序中的函数使用。

Now when I want to use this library in another program, do I need to include all of the header files that were used originally for building the library, or including just the headers that belong to used functions in my (new) program suffices? There are many functions in the library that are not used directly in my new program, but, needless to say, all of them are used by the functions in the program.

(因此,它们在程序中也间接使用,如果不包含它们将会很奇怪。)

(So they are indirectly used in the program too, and it would be strange if their inclusion is not necessary.)

推荐答案

头文件不是什么神奇的,它们只是一种方便的方法来确保您的 .cpp 文件可以访问所有它需要的声明和定义。所以只包括你在每个文件(1)中实际需要的。

Header files are nothing magical, they're just a convenient way of ensuring your .cpp file has access to all the declarations and definitions it needs. So only include those you actually need in each of your files(1).

上面提到的是 include 语句在您的文件 - 只为头文件,你实际需要使用。当然,您包含的头文件可以<$ p $ c> #include 其他头文件本身。

The above refers to putting #include statements in your files—only do this for header files you actually need to use. Of course, the header files you include can #include other header files themselves.

(即实际可用的)DLL,您的DLL必须提供一组公共接口头文件。在正常情况下,您必须具有所有这些公共接口头文件可用于使用该DLL构建程序。这不意味着你应该 #include them all;恰恰相反,你不应该。编译器(实际上是预处理器)会 #include 它需要什么。

Like any other well-behaved (i.e., actually usable) DLL, your DLL must supply a set of public interface header files. Under normal circumstances, you must have all these public interface header files available for building a program using that DLL. That doesn't mean you should #include them all; quite on the contrary, you should not. The compiler (preprocessor, actually) will #include what it needs.

请注意, ,可能有更多的头文件涉及,但这些是私有的实现,而不是公共接口的一部分。你不需要他们使用DLL来构建程序,在一般情况下你甚至不能访问它们。

Note that when the DLL itself was built, there were probably many more header files involved, but these were private to the implementation and not part of the public interface. You do not need them to build a program using the DLL, and in the general case you don't even have access to them.

所以,当设计你自己的DLL ,您必须严格区分公共和内部头文件。公共头文件必须对客户端可用,内部不需要(除非客户端应该自己构建DLL)。当然,这意味着公共头文件可能永远不会 #include 一个内部头文件。

So, when designing your own DLL, you must stricly differentiate between public and internal header files. The public header files must be made available to clients, the internal ones need not (unless the client is supposed to build the DLL themselves). Of course, this means a public header file may never #include an internal one.

(1)请注意,头文件包含是纯文本的 - 当读取源文件时,预处理器有效地复制头文件的内容并粘贴它代替 #include 语句,然后进行解析。你做这个复制&粘贴自己相反,该文件将构建和运行,以及。当然,它不会拾取头文件中的更改,这是头文件首先被使用的主要原因。

(1) Note that header file inclusion is purely textual—when reading your source file, the preprocessor effectively copies the contents of the header file and pastes it in place of the #include statement, then goes on parsing. You you did this copy & paste yourself instead, the file would build and run just as well. Of course, it wouldn't pick up later changes in the header file, which is the primary reason why header files are used in the first place.

这篇关于当使用dll库,是否有必要包括用于构建dll的所有标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆