简体   繁体   English

如何使用gcc使用源代码文件编译库归档文件?

[英]How can I compile a library archive with a source code file with gcc?

TL;DR - I need to compile archive.a with test.o to make an executable. TL; DR-我需要使用test.o编译archive.a来生成可执行文件。

Background - I am trying to call a function in a separate library from a software package I am modifying but the function (a string parser) is creating a segmentation violation. 背景-我正在尝试从要修改的软件包中的单独库中调用函数,但该函数(字符串解析器)正在创建分段违规。 The failure is definitely happening in the library and the developer has asked for a test case where the error occurs. 该故障肯定是在库中发生的,并且开发人员已要求一个发生错误的测试用例。 Rather than having him try to compile the rather large software package that I'm working on I'd rather just send him a simple program that calls the appropriate function (hopefully dying at the same place). 与其让他尝试编译我正在使用的相当大的软件包,不如给他发送一个简单的程序来调用适当的函数(希望在同一个地方死去)。 His library makes use of several system libraries as well (lapack, cblas, etc.) so the linking needs to hit everything I think. 他的库还利用了几个系统库(lapack,cblas等),因此链接需要满足我的所有想法。

I can link to the .o files that are created when I make his library but of course they don't link to the appropriate system libraries. 我可以链接到在创建他的库时创建的.o文件,但它们当然不会链接到适当的系统库。

This seems like it should be straight forward, but it's got me all flummoxed. 这似乎应该是直截了当的,但是这让我都感到困惑。

The .a extension indicates that it is a static library. .a扩展名表明它是一个静态库。 So in order to link against it you can use the switches for the linking stage: 因此,为了链接它,您可以在链接阶段使用开关:

gcc -o myprog -L<path to your library> main.o ... -larchive

Generally you use -L to add the path where libraries are stored (unless it is in the current directory) and you use -l<libname> to sepecify a library. 通常,您使用-L添加存储库的路径(除非它在当前目录中),并使用-l<libname>分隔一个库。 The libraryname is without extension. 库名没有扩展名。 If the library is named libarchive.a you would still give -larchive. 如果该库名为libarchive.a,您仍将使用-larchive。 If you want to specify the full name of the library, then you would use ie -l:libname.a 如果要指定库的全名,则可以使用-l:libname.a

update 更新

If the libraypath is /usr/lib/libmylibrary.a you would use 如果libraypath是/usr/lib/libmylibrary.a ,则可以使用

-L/usr/lib -lmylibrary

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

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