0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
William Everett
  • 751
  • 1
  • 8
  • 18

1 Answers1

5

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
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • I tried: `gcc -o main -Llibname main.o` but I am getting "undefined reference to" errors, despite including the appropriate headers and knowing that the functions being called are present in the library. – William Everett Jun 10 '13 at 17:56
  • Try to list main.o first and afterwards the -l switch. the order can matter. Also note it's -l not -L. If it doesn't work, post the actual commandline you are using. – Devolus Jun 10 '13 at 18:07
  • gcc -o test test.o -llibefp gives me: `/usr/bin/ld: cannot find -llibefp collect2: error: ld returned 1 exit status` The library is in the directory that I'm working in, which is why I went with -L instead of the -l. Doing: `gcc -o test test.o -Llibefp` yields: `test.o: In function 'main': test.c:(.text+0x13): undefined reference to 'efp_create' test.c:(.text+0x109): undefined reference to 'efp_add_potential' test.c:(.text+0x116): undefined reference to 'efp_result_to_string' collect2: error: ld returned 1 exit status` – William Everett Jun 10 '13 at 18:34
  • 1
    See my update. `-lefp` if the library is named `libefp.a` or `libfpe.so` If the library is in the current directory you don't need to give -L as the current directory is always searched by the linker. – Devolus Jun 10 '13 at 18:39
  • `gcc -o test test.o -lefp` fives `/usr/bin/ld: cannot find -lefp` (the name of the file is libefp.a. Trying `gcc -o test test.o libefp.a -lcblas -llapack` got it to compile and run as expected though. Thanks for your help. – William Everett Jun 10 '13 at 18:45