问题描述
假设我有 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.
这篇关于如何创建库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!