简体   繁体   English

链接库以创建静态库

[英]linking library for creating static library

I have written some code in Lib_file.h and Lib_file.cpp. 我在Lib_file.h和Lib_file.cpp中编写了一些代码。 I wish to convert this code to a static library. 我希望将此代码转换为静态库。 I am able to compile the code (using the command g++ -I <necessary include files> -o Lib_file.o Lib_file.cpp )to get Lib_file.o. 我能够编译代码(使用命令g++ -I <necessary include files> -o Lib_file.o Lib_file.cpp )来获取Lib_file.o。 I am also able to add it to an archive using the ar rvs Lib_file.a Lib_file.o command. 我还可以使用ar rvs Lib_file.a Lib_file.o命令将其添加到存档。 Now when I try to use this library in some other code using the -L option, I get undefined reference errors. 现在,当我尝试使用-L选项在其他代码中使用此库时,我得到未定义的引用错误。 This errors point to the code in my Lib_file.o . 这个错误指向我的Lib_file.o中的代码。 So my question is how do I get the code in my Lib_file.cpp to link to the libraries that it uses. 所以我的问题是如何让我的Lib_file.cpp中的代码链接到它使用的库。

I have tried the following options so far 到目前为止,我尝试了以下选项

I. After creating the Lib_file.o, I tried the following command g++ -L<include path> -l<.a files> Lib_file.o . I.创建Lib_file.o后,我尝试了以下命令g++ -L<include path> -l<.a files> Lib_file.o On executing this command, I get the following error 执行此命令时,出现以下错误

/usr/lib/../lib64/crt1.o: In function `_start':
init.c:(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status

II. II。 I tried to include all the necessary .a files in a new archive along with my Lib_file.o using the ar command. 我尝试使用ar命令将所有必需的.a文件与我的Lib_file.o一起包含在新存档中。 Still I get the undefined reference error when I try to use the Lib_file.a library with my application 当我尝试在我的应用程序中使用Lib_file.a库时,仍然会收到未定义的引用错误

Please help me out here 请帮帮我

First of all, all libraries are normally named something like libxyz.a where xyz is the name of the library. 首先,所有库通常都命名为libxyz.a ,其中xyz是库的名称。

Secondly, you try to create a program using only the object file you used for the library, and also linking it with itself. 其次,您尝试仅使用用于库的目标文件创建程序,并将其与自身链接。 This will of course not work, since the library have no main function which is needed for normal programs. 这当然行不通,因为该库没有正常程序所需的main功能。 You have to create another program, and link that one with the library. 您必须创建另一个程序,并将程序与库链接。

Like 喜欢

gcc myotherprogram.c -o myotherprogram -L/some/path -lxyz

As you can see in my command line above, I placed the library last on the command line. 正如您在上面的命令行中看到的那样,我将库放在命令行的最后 It's needed because the linker look for dependencies in kind of reversed order. 这是必需的,因为链接器以相反的顺序寻找依赖关系。

Edit: Linking your static library with other libraries: You don't. 编辑:将静态库与其他库链接:您没有。 A static library is completely standalone, and if it needs other libraries itself to work then they have to be present on the command line when compiling the actual program. 静态库是完全独立的,如果它需要其他库本身才能工作,那么在编译实际程序时它们必须出现在命令行中。

For example, lets say that library xyz depends on the standard math library (ie the m library). 例如,假设库xyz依赖于标准数学库(即m库)。 You can't "link" with it when creating the xyz library as you don't actually link static libraries, you just put a collection of object files together in an archive ( ar and the .a extension is for archive). 在创建xyz库时,您不能“链接”它,因为您实际上没有链接静态库,您只需将一组目标文件放在一个存档中( ar.a扩展名用于存档)。 When you build the actual application that needs the xyz library you also needs to link with whatever libraries that xyz needs: 当您构建需要xyz库的实际应用程序时,您还需要链接xyz需要的任何库:

gcc myotherprogram.c -o myotherprogram -L/some/path -lxyz -lm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM