C++ 在 UNIX 中创建一个项目,从 Makefile 到静态/动态库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/598948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:26  来源:igfitidea点击:

Creating a project, from Makefile to static/dynamic libraries in UNIX

c++cmakefileunix

提问by

Guys, would you describe a few things about c++ building blocks, on unix.

伙计们,你能在 unix 上描述一些关于 c++ 构建块的事情吗?

I want to create an application that links against static libs and dynamic libs (.so).

我想创建一个链接静态库和动态库 (.so) 的应用程序。

Question 1:How do I create static library using gcc/g++ ?How do I make my app link against it.

问题 1:如何使用 gcc/g++ 创建静态库?如何使我的应用程序链接到它。

Question 2:How to specify it in the makefile, linking against static and dynamic libs, assuming that both libraries have header files

问题2:如何在makefile中指定,针对静态库和动态库进行链接,假设两个库都有头文件

Summary: I have been using makefiles and libraries for years, written by someone else. Thus every time I modified it, I simply cut-and-pasted things around, without really understanding it. Now I want to get down to the ground and understand the building/linking/Creating Makfile process in-depth.

简介: 我多年来一直在使用由其他人编写的 makefile 和库。所以每次修改的时候,我只是简单地把东西复制粘贴,并没有真正理解它。现在我想深入了解构建/链接/创建 Makfile 的过程。

What is a good book describing these concepts in intimate details?

什么是一本详细描述这些概念的好书?

Thanks

谢谢

采纳答案by greyfade

Static libraries are usually archivedwith the arcommand. Once you build all of the object files (preferably with the -fPICswitch on GCC), you can run arlike so:

静态库通常使用该命令存档ar。一旦你构建了所有的目标文件(最好使用-fPICGCC上的开关),你可以ar像这样运行:

ar -rs archivename.a list.o of.o objects.o

The manpage describes the options.

man页面描述了选项。

Dynamic libraries are built usually with the -sharedswitch to gccor ldand the output file name with a .soextension.

动态库通常是通过-shared切换到gccld以及带有.so扩展名的输出文件名来构建的。

Autotools handles this with the libtoolprogram. I'm not familiar with its use.

Autotools 用libtool程序处理这个问题。我不熟悉它的用途。

Linking against these libraries can be done either by listing the libraries with the -l(ell) switch (such as -lXto link to libX.so) or by specifying them directly with absolute paths (such as adding /usr/lib/libX.soto your command). Static libraries are linked by specifying -staticbefore -lor the appropriate absolute path to the .aarchive.

链接这些库可以通过使用-l(ell) 开关列出库(例如-lXto link to libX.so)或直接使用绝对路径指定它们(例如添加/usr/lib/libX.so到您的命令)来完成。静态库通过在存档-static之前指定-l或适当的绝对路径来链接.a

回答by Paul Beckingham

Bare bones Makefile for creating a static library consisting of the code in foo.cpp, bar.cpp:

用于创建由 foo.cpp、bar.cpp 中的代码组成的静态库的裸骨 Makefile:

PROJECT = library.a
OBJECTS = foo.o bar.o
CFLAGS  = -Wall -pedantic

all: $(PROJECT)

.cpp.o:
        g++ -c $(CFLAGS) $<

$(PROJECT): $(OBJECTS)
        libtool -o $(PROJECT) -static $(OBJECTS)

Bare bones Makefile for an app baz.cpp that static links to library.a:

静态链接到 library.a 的应用程序 baz.cpp 的裸机 Makefile:

PROJECT = baz
CFLAGS  = -Wall -pedantic
OBJECTS = baz.o

all: $(PROJECT)

.cpp.o:
        g++ -c $(CFLAGS) $<

$(PROJECT): $(OBJECTS) library.a
        g++ $(OBJECTS) -L. -llibrary -o $(PROJECT)

Dynamic library left, ahem, as an exercise to the reader.

动态库离开了,咳咳,作为给读者的练习。

回答by Paul Beckingham

Answer 1: To create a static library from source files foo.c and bar.c, do this:

答案 1:要从源文件 foo.c 和 bar.c 创建静态库,请执行以下操作:

gcc -c foo.c
gcc -c bar.c
ar rc mylibrary.a foo.o bar.o

For more information about this, read the GCC manualmanual to learn how to use the compiler, and the linker via the compiler. The binutils manualshould also be helpful.

有关这方面的更多信息,请阅读GCC 手册以了解如何通过编译器使用编译器和链接器。该 binutils的手册也应该是有帮助的。

Answer 2: The GNU Make manualis pretty good. To really learn about libraries and how they work, read the Linkers and Loadersbook by John R. Levine.

答案 2:GNU Make 手册非常好。要真正了解库及其工作原理,请阅读 John R. Levine的Linkers and Loaders一书。

Static libraries are pretty simple, but shared libraries can be very hairy, depending on the platform and the amount of portability you want and need. As an example, on some systems static and shared libraries must be compiled with different options to work properly (one must and the other must not be compiled with position independent code). Whole frameworks of utilities have been developed to make this easier (libtool), but they are not unproblematic themselves.

静态库非常简单,但共享库可能非常麻烦,这取决于平台和您想要和需要的可移植性。例如,在某些系统上,静态库和共享库必须使用不同的选项进行编译才能正常工作(必须使用与位置无关的代码编译另一个必须使用的选项)。已经开发了整个实用程序框架来简化此操作(libtool),但它们本身并非没有问题。

回答by Christoffer

Since you refer to gcc, I assume you're using GNU Make. The best documentation I've found for that is the official manual, which covers everything you need to know in easy-to-understand terms.

由于您指的是 gcc,我假设您使用的是 GNU Make。我找到的最好的文档是官方手册,它以易于理解的方式涵盖了您需要了解的所有内容。

回答by aks

When I was learning about Linux programming, Advanced Linux Programminghelped me a lot. You can check Writing and Using Libraries section in this pdf. It explains quite a bit about libraries in Linux.

在我学习 Linux 编程时,Advanced Linux Programming对我帮助很大。您可以查看此 pdf 中的编写和使用库部分。它解释了很多关于 Linux 中的库。

回答by user314463