<i id='oQ7h0'><tr id='oQ7h0'><dt id='oQ7h0'><q id='oQ7h0'><span id='oQ7h0'><b id='oQ7h0'><form id='oQ7h0'><ins id='oQ7h0'></ins><ul id='oQ7h0'></ul><sub id='oQ7h0'></sub></form><legend id='oQ7h0'></legend><bdo id='oQ7h0'><pre id='oQ7h0'><center id='oQ7h0'></center></pre></bdo></b><th id='oQ7h0'></th></span></q></dt></tr></i><div id='oQ7h0'><tfoot id='oQ7h0'></tfoot><dl id='oQ7h0'><fieldset id='oQ7h0'></fieldset></dl></div>

    • <bdo id='oQ7h0'></bdo><ul id='oQ7h0'></ul>

    <small id='oQ7h0'></small><noframes id='oQ7h0'>

  • <legend id='oQ7h0'><style id='oQ7h0'><dir id='oQ7h0'><q id='oQ7h0'></q></dir></style></legend>
      <tfoot id='oQ7h0'></tfoot>

      1. 如何创建库?

        时间:2023-10-17

          <small id='H7b84'></small><noframes id='H7b84'>

          • <legend id='H7b84'><style id='H7b84'><dir id='H7b84'><q id='H7b84'></q></dir></style></legend>

                <tbody id='H7b84'></tbody>
            1. <tfoot id='H7b84'></tfoot>

                  <bdo id='H7b84'></bdo><ul id='H7b84'></ul>
                • <i id='H7b84'><tr id='H7b84'><dt id='H7b84'><q id='H7b84'><span id='H7b84'><b id='H7b84'><form id='H7b84'><ins id='H7b84'></ins><ul id='H7b84'></ul><sub id='H7b84'></sub></form><legend id='H7b84'></legend><bdo id='H7b84'><pre id='H7b84'><center id='H7b84'></center></pre></bdo></b><th id='H7b84'></th></span></q></dt></tr></i><div id='H7b84'><tfoot id='H7b84'></tfoot><dl id='H7b84'><fieldset id='H7b84'></fieldset></dl></div>
                • 本文介绍了如何创建库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设我有 10 个 *.hpp 和 *.cpp 文件需要编译代码.我知道对于许多不同的代码,我将需要这些相同的文件.我可以用这些文件创建一个包",让我可以简单地编写:

                  Let's say I have 10 *.hpp and *.cpp files that I need to compile a code. I know that I will need those same files for many different codes. Can I create a "package" with those files that would allow me to simply write:

                  #include <mypackage>
                  

                  代替:

                  #include "file1.hpp"
                  #include "file2.hpp"
                  ...
                  #include "file10.hpp"
                  

                  我不需要每次需要这个包"时都编写一个makefile.

                  I wouldn't then need to write a makefile every time I need this "package".

                  更准确地说,我使用的是 Linux.

                  To be more precise, I use Linux.

                  推荐答案

                  CPP 源(H 文件和 CPP 文件)的集合可以一起编译到一个库"中,然后可以在其他程序和库中使用.如何做到这一点的细节是特定于平台和工具链的,所以我把它留给你去发现细节.不过,我会提供几个链接供您阅读:

                  A collection of CPP sources (H files and CPP files) can be compiled together in to a "library," which can then be used in other programs and libraries. The specifics of how to do this are platform- and toolchain-specific, so I leave it to you to discover the details. However, I'll provide a couple links that you can have a read of:

                  使用 gnu 编译器 [gcc] 创建共享和静态库

                  演练:创建和使用动态链接库 (C++)

                  库可以分为两种类型:源代码库和二进制库.也可以是这两种类型的混合体——一个库既可以是源库,也可以是二进制库.源代码库很简单:作为源代码分发的代码集合;通常是头文件.大多数 Boost 库都属于这种类型.二进制库被编译成一个可由客户端程序运行时加载的包.

                  Libraries can be seperated in to two types: source code libraries, and binary libraries. There can also be hybrids of these two types -- a library can be both a source and binary library. Source code libraries are simply that: a collection of code distributed as just source code; typically header files. Most of the Boost libraries are of this type. Binary libraries are compiled in to a package that is runtime-loadable by a client program.

                  即使在二进制库的情况下(显然在源库的情况下),也必须向库的用户提供一个头文件(或多个头文件).这告诉客户端程序的编译器要在库中查找哪些函数等.库编写者经常做的是一个单一的主头文件,它由库导出的所有内容的声明组成,客户端将 #include 该头文件.稍后,在二进制库的情况下,客户端程序将链接"到库,这会将标题中提到的所有名称解析为可执行地址.

                  Even in the case of binary libraries (and obviously in the case of source libraries), a header file (or multiple header files) must be provided to the user of the library. This tells the compiler of the client program what functions etc to look for in the library. What is often done by library writers is a single, master header file is composed with declarations of everything that is exported by the library, and the client will #include that header. Later, in the case of binary libraries, the client program will "link" to the library, and this resolves all the names mentioned in the header to executable addresses.

                  在编写客户端头文件时,请记住复杂性.可能在很多情况下,您的一些客户只想使用您图书馆的少数几个部分.如果您编写一个包含库中所有内容的主头文件,您的客户端编译时间将不必要地增加.

                  When composing the client-side header file, keep complexity in mind. There may be many cases where some of your clients only want to use some few parts of your library. If you compose one master header file that includes everything from your library, your clients compilation times will be needlessly increased.

                  处理这个问题的常用方法是为库的相关部分提供单独的头文件.如果您将所有 Boost 视为单个库,那么 Boost 就是一个例子.Boost 是一个庞大的库,但如果您只需要正则表达式功能,则只能#include 与正则表达式相关的标头来获得该功能.如果您只需要正则表达式,则不必包含 所有 Boost.

                  A common way of dealing with this problem is to provide individual header files for correlated parts of your library. If you think of all of Boost a single library, then Boost is an example of this. Boost is an enormous library, but if all you want is the regex functionality, you can only #include the regex-related header(s) to get that functionality. You don't have to include all of Boost if all you want is the regex stuff.

                  在 Windows 和 Linux 下,二进制库可以进一步细分为两种类型:动态和静态.在静态库的情况下,库的代码实际上是导入"(因为没有更好的术语)到客户端程序的可执行文件中.静态库由您分发,但这仅在编译步骤期间客户端需要.当您不想强迫您的客户必须随他们的程序分发其他文件时,这很方便.它还有助于避免依赖地狱.另一方面,动态库不是直接导入"到客户端程序中,而是在客户端程序执行时动态加载.这既减少了客户端程序的大小,也减少了在多个程序使用相同动态库的情况下可能的磁盘占用空间,但库二进制文件必须是分布式的.与客户端程序一起安装.

                  Under both Windows and Linux, binary libraries can be further subdivided in to two types: dynamic and static. In the case of static libraries, the code of the library is actually "imported" (for lack of a better term) in to the executable of the client program. A static library is distributed by you, but this is only needed by the client during the compilation step. This is handy when you do not want to force your client to have to distribute additional files with their program. It also helps to avoid Dependancy Hell. A Dynamic library, on the other hand, is not "imported" in to the client program directly, buy dynamically loaded by the client program when it executes. This both reduces the size of the client program and potentially the disc footprint in cases where multiple programs use the same dynamic library, but the library binary must be distributed & installed with the client program.

                  这篇关于如何创建库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何加快 g++ 编译时间(使用大量模板时) 下一篇:是否可以在不使用 main() 函数的情况下编写程序?

                  相关文章

                  <i id='ADkTH'><tr id='ADkTH'><dt id='ADkTH'><q id='ADkTH'><span id='ADkTH'><b id='ADkTH'><form id='ADkTH'><ins id='ADkTH'></ins><ul id='ADkTH'></ul><sub id='ADkTH'></sub></form><legend id='ADkTH'></legend><bdo id='ADkTH'><pre id='ADkTH'><center id='ADkTH'></center></pre></bdo></b><th id='ADkTH'></th></span></q></dt></tr></i><div id='ADkTH'><tfoot id='ADkTH'></tfoot><dl id='ADkTH'><fieldset id='ADkTH'></fieldset></dl></div>

                  1. <tfoot id='ADkTH'></tfoot>

                      <bdo id='ADkTH'></bdo><ul id='ADkTH'></ul>
                      <legend id='ADkTH'><style id='ADkTH'><dir id='ADkTH'><q id='ADkTH'></q></dir></style></legend>
                    1. <small id='ADkTH'></small><noframes id='ADkTH'>