C++ 如何使用 g++ 创建静态库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5947067/
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-28 19:13:22  来源:igfitidea点击:

How to create a static library with g++?

c++gcccompiler-constructiong++

提问by linuxx

Can someone please tell me how to create a static library from a .cpp and a .hpp file? Do I need to create the .o and the .a? I would also like to know how can I compile a static library in and use it in other .cpp code. I have header.cpp, header.hpp .I would like to create header.a. Test the header.a in test.cpp. I am using g++ for compiling.

有人可以告诉我如何从 .cpp 和 .hpp 文件创建静态库吗?我需要创建 .o 和 .a 吗?我还想知道如何编译静态库并在其他 .cpp 代码中使用它。我有header.cppheader.hpp .我想创造header.a。测试中的 header.a test.cpp。我正在使用 g++ 进行编译。

回答by

Create a .o file:

创建一个 .o 文件:

g++ -c header.cpp

add this file to a library, creating library if necessary:

将此文件添加到库中,必要时创建库:

ar rvs header.a header.o

use library:

使用库:

g++ main.cpp header.a

回答by Sriram

You can create a .afile using the arutility, like so:

您可以.a使用该ar实用程序创建一个文件,如下所示:

ar crf lib/libHeader.a header.o

libis a directory that contains all your libraries. it is good practice to organise your code this way and separate the code and the object files. Having everything in one directory generally looks ugly. The above line creates libHeader.ain the directory lib. So, in your current directory, do:

lib是一个包含所有库的目录。以这种方式组织代码并将代码和目标文件分开是一种很好的做法。将所有内容放在一个目录中通常看起来很难看。上面的行在libHeader.a目录中创建lib。因此,在您当前的目录中,执行以下操作:

mkdir lib

Then run the above arcommand.

然后运行上面的ar命令。

When linking all libraries, you can do it like so:

链接所有库时,您可以这样做:

g++ test.o -L./lib -lHeader -o test  

The -Lflag will get g++to add the lib/directory to the path. This way, g++knows what directory to search when looking for libHeader. -llibHeaderflags the specific library to link.

-L标志将把目录g++添加lib/到路径中。这样,g++在查找libHeader. -llibHeader标记要链接的特定库。

where test.o is created like so:

test.o 是这样创建的:

g++ -c test.cpp -o test.o 

回答by Lightness Races in Orbit

Can someone please tell me how to create a static library from a .cpp and a .hpp file? Do I need to create the .o and the the .a?

有人可以告诉我如何从 .cpp 和 .hpp 文件创建静态库吗?我需要创建 .o 和 .a 吗?

Yes.

是的。

Create the .o(as per normal):

创建 .o(按照正常方式):

g++ -c header.cpp

Create the archive:

创建存档

ar rvs header.a header.o

Test:

测试

g++ test.cpp header.a -o executable_name


Note that it seems a bit pointless to make an archive with just one module in it. You could just as easily have written:

请注意,制作一个只有一个模块的存档似乎有点无意义。你可以很容易地写:

g++ test.cpp header.cpp -o executable_name

Still, I'll give you the benefit of the doubt that your actual use case is a bit more complex, with more modules.

尽管如此,我还是会给你一个好处,即你的实际用例有点复杂,有更多的模块。

Hope this helps!

希望这可以帮助!