静态库,但我还需要头? [英] static library, but I still need headers?

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

问题描述

我有一堆项目,可以共享一个常见的静态库类。

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如何处理这个问题? / p>

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.

有关静态库(优点,比较动态等)的详情:

其中,很高兴将你的项目分成库,这样你就不会遇到一个庞大的整体项目。

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.

这篇关于静态库,但我还需要头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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