Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking C library with non-standard name

Tags:

c

gcc

linker

I am using gcc to compile a program which I need to link to a C library with non-standard name; it is called stuff.a instead of libstuff.a.

I cannot change the name of the file (permission issues).

I don't want to include the full library (i.e. using gcc program.c stuff.a -oprogram)

I want to compile as gcc program.c -L/path/to/library/ -lstuff -oprogram but gcc will not find the library (because it is not called libstuff.a).

I am working on a Linux box.

How can I get the (dynamic) linking done?

EDIT:

Thank you all, and my apologies for a poorly worded question.

I did not even have a shared object (I thought I could link dynamically to an *.a file), so this confused many of you. Again, apologies for my ignorance.

What I ended up doing is creating the shared object in a local directory, appending the location to my LD_LIBRARY_PATH environment variable, and linking again.

It works like a charm (from 1.3M executable down to 5.8K).

Thanks again.

like image 274
Escualo Avatar asked Jan 21 '10 19:01

Escualo


3 Answers

You should have taken a look at the gcc manual:

The only difference between using an -l option and specifying a file name is that -l surrounds library with 'lib' and '.a' and searches several directories.

There's nothing wrong with using stuff.a as argument.

like image 187
Christoph Avatar answered Nov 19 '22 08:11

Christoph


Assuming that a shared object version of the static library does not exist, it might be necessary to create one. Remember that the static library stuff.a is just an ar archive.

ar -x stuff.a
gcc -shared *.o -o libstuff.so

This assumes you want to link against it as a shared library and not simply compile it into your binary.

like image 40
jschmier Avatar answered Nov 19 '22 10:11

jschmier


I know the problem turned out to be one with trying to set a static library as dynamically linked, but I wanted to add this for posterity's sake:

If you preface your argument to the -l option with a colon, :, it will treat the name as literal rather than a name needing the "lib" added to the front and the file extension added to the end.

In the case you describe where you want to link against a static.a rather than a libstatic.a, and assuming you do intend to link against it statically, the the following would work:

 gcc program.c -L/path/to/library/ -l:stuff.a -oprogram

You could of course do as Richard Penington and Anycorn have both mentioned and simply include the fully-pathed library on the command line instead as well:

gcc program.c /path/to/library/stuff.a -oprogram
like image 6
mtalexan Avatar answered Nov 19 '22 08:11

mtalexan