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?
那么静态库有什么好处呢?
How do companies like Adobe deal with this?
像Adobe这样的公司如何处理这个?
3 个解决方案
#1
34
Static libraries allow you to create a library and use that library in many projects.
静态库允许您创建库并在许多项目中使用该库。
The need for header files:
需要头文件:
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.
编译器仅将源代码作为输入并生成输出。它不处理输入时编译的目标文件或静态库。
The need for linking in the library:
需要在库中链接:
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.
链接器获取所有目标文件(已编译的代码)以及所有静态库,并生成可执行文件或二进制文件。
More info about static libraries (benefits, comparing dynamic, etc...):
有关静态库(好处,比较动态等)的更多信息:
Amongst other things, it is nice to separate your project into libraries so that you don't end up with 1 huge monolithic project.
除此之外,将项目分成库是很好的,这样你就不会得到一个巨大的整体项目。
You do not need to distribute the source code (typically in the .cpp files) this way.
您不需要以这种方式分发源代码(通常在.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.
如果您只是在每个使用公共库的项目中包含所有.cpp文件,那么每次都必须编译.cpp文件。
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.
静态库相对于动态库的缺点包括您的文件大小会更大,因为每个可执行文件都需要自己的副本,并且您不能交换不同版本的库,因为它不是动态加载的。
Re your question: How do companies deal with this:
问你的问题:公司如何应对这个问题:
A typical company will make use of both static and dynamic libraries extensively.
典型的公司将广泛使用静态和动态库。
#2
2
The typical way you make use of a static library is to have a target in your Makefile (or whatever build system you use) that installs the headers into an appropriate location at the same time that it installs the library.
使用静态库的典型方法是在Makefile(或您使用的任何构建系统)中创建一个目标,该目标在安装库的同时将标头安装到适当的位置。
So, your static library ends up in /usr/local/lib, and the headers go into /usr/local/include or wherever.
因此,您的静态库最终在/ usr / local / lib中,并且标头进入/ usr / local / include或wherever。
#3
0
Also, when compared with linking against object files, linking against static library may result is a smaller final executable. The reason for this is, if you don't call any of the functions from a particular object file (included in the static library), the linker will not include the code for those functions in you final executable. See Extraneous Library Linkage
此外,与链接目标文件相比,与静态库链接可能会导致较小的最终可执行文件。原因是,如果不调用特定目标文件(包含在静态库中)中的任何函数,链接器将不会在最终可执行文件中包含这些函数的代码。请参阅Extraneous Library Linkage
#1
34
Static libraries allow you to create a library and use that library in many projects.
静态库允许您创建库并在许多项目中使用该库。
The need for header files:
需要头文件:
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.
编译器仅将源代码作为输入并生成输出。它不处理输入时编译的目标文件或静态库。
The need for linking in the library:
需要在库中链接:
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.
链接器获取所有目标文件(已编译的代码)以及所有静态库,并生成可执行文件或二进制文件。
More info about static libraries (benefits, comparing dynamic, etc...):
有关静态库(好处,比较动态等)的更多信息:
Amongst other things, it is nice to separate your project into libraries so that you don't end up with 1 huge monolithic project.
除此之外,将项目分成库是很好的,这样你就不会得到一个巨大的整体项目。
You do not need to distribute the source code (typically in the .cpp files) this way.
您不需要以这种方式分发源代码(通常在.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.
如果您只是在每个使用公共库的项目中包含所有.cpp文件,那么每次都必须编译.cpp文件。
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.
静态库相对于动态库的缺点包括您的文件大小会更大,因为每个可执行文件都需要自己的副本,并且您不能交换不同版本的库,因为它不是动态加载的。
Re your question: How do companies deal with this:
问你的问题:公司如何应对这个问题:
A typical company will make use of both static and dynamic libraries extensively.
典型的公司将广泛使用静态和动态库。
#2
2
The typical way you make use of a static library is to have a target in your Makefile (or whatever build system you use) that installs the headers into an appropriate location at the same time that it installs the library.
使用静态库的典型方法是在Makefile(或您使用的任何构建系统)中创建一个目标,该目标在安装库的同时将标头安装到适当的位置。
So, your static library ends up in /usr/local/lib, and the headers go into /usr/local/include or wherever.
因此,您的静态库最终在/ usr / local / lib中,并且标头进入/ usr / local / include或wherever。
#3
0
Also, when compared with linking against object files, linking against static library may result is a smaller final executable. The reason for this is, if you don't call any of the functions from a particular object file (included in the static library), the linker will not include the code for those functions in you final executable. See Extraneous Library Linkage
此外,与链接目标文件相比,与静态库链接可能会导致较小的最终可执行文件。原因是,如果不调用特定目标文件(包含在静态库中)中的任何函数,链接器将不会在最终可执行文件中包含这些函数的代码。请参阅Extraneous Library Linkage