如何创建图书馆? [英] How do I create a library?

查看:91
本文介绍了如何创建图书馆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我有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的 all .

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.

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

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