静态库,但我仍然需要标题?

static library, but I still need headers?(静态库,但我仍然需要标题?)

本文介绍了静态库,但我仍然需要标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆项目,它们都可以共享一个通用"静态类库.

I have a bunch of projects that all could share a "common" static library of classes.

让我感到困惑的是,如果我从这些类中创建一个静态库并在我的项目中链接它,我仍然需要在我的主要项目中静态库中的类的标题.

What confuses me is if I make a static library out of these classes and link against it in my projects that I still need the headers of the classes in the static library in my main projects.

那么静态库有什么好处呢?

What is the benefit of the static library then?

像 Adob​​e 这样的公司如何处理这个问题?

How do companies like Adobe deal with this?

推荐答案

静态库允许您创建一个库并在许多项目中使用该库.

Static libraries allow you to create a library and use that library in many projects.

需要头文件:

由于使用库的项目是独立于库进行编程和编译的,因此该程序需要知道您正在使用的东西的声明.否则你的编译器怎么知道你正在编写有效的代码?

Since the project using the library is programmed and compiled independent of the library, that program needs to know the declaration of the things you're using. Otherwise how would your compiler know you're writing valid code?

编译器只将源代码作为输入并产生输出.它不处理输入时编译的目标文件或静态库.

A compiler only takes source code as input and produces output. It does not deal with compiled object files or static libraries on input.

需要在库中进行链接:

因此,拥有标头可以让您在项目中编写有效代码,但在链接时间方面,您需要提供包含在静态库中的定义.

So having the headers allows you to write valid code in your project, but when it comes to link time you'll need to provide the definition which is contained inside the static library.

链接器获取所有目标文件(编译代码)以及所有静态库并生成可执行文件或二进制文件.

The linker takes all object files (compiled code) and also all static libraries and produces an executable or binary.

有关静态库的更多信息(优点、比较动态等...):

除其他外,最好将您的项目分成库,这样您就不会得到 1 个巨大的单体项目.

Amongst other things, it is nice to separate your project into libraries so that you don't end up with 1 huge monolithic project.

您不需要以这种方式分发源代码(通常在 .cpp 文件中).

You do not need to distribute the source code (typically in the .cpp files) this way.

如果您只是在每个使用公共库的项目中包含所有 .cpp 文件,那么您每次都必须编译 .cpp 文件.

If you were to simply include all the .cpp files in every project that used the common library then you would have to compile the .cpp files each time.

静态库相对于动态库的一个优势是,您始终可以确保您的程序是自包含的,并且它们使用的是正确版本的库(因为它们被编译成可执行文件本身).与动态链接相比,您还将获得轻微的速度优势.

An advantage of static libraries over dynamic libraries is that you can always be sure that your programs will be self contained and that they are using the correct version of the library (since they are compiled into the executable itself). You will also have a slight speed advantage over dynamic linking.

静态库相对于动态库的缺点包括您的文件会更大,因为每个可执行文件都需要自己的副本,而且您不能换出不同版本的库,因为它不是动态加载的.

Disadvantages of static libraries over dynamic libraries include that your file sizes will be bigger because each executable needs their own copy, and that you can't swap out a different version of the library since it's not dynamically loaded.

你的问题:公司如何处理这个问题:

典型的公司会广泛使用静态库和动态库.

A typical company will make use of both static and dynamic libraries extensively.

这篇关于静态库,但我仍然需要标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:静态库,但我仍然需要标题?

基础教程推荐