質問

TL;DR - I need to compile archive.a with test.o to make an executable.

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.

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.

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. 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. The libraryname is without extension. If the library is named libarchive.a you would still give -larchive. If you want to specify the full name of the library, then you would use i.e. -l:libname.a

update

If the libraypath is /usr/lib/libmylibrary.a you would use

-L/usr/lib -lmylibrary
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top