Escualo
Escualo

Reputation: 42072

Linking C library with non-standard name

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.

Upvotes: 7

Views: 10248

Answers (6)

mtalexan
mtalexan

Reputation: 763

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

Upvotes: 6

Christoph
Christoph

Reputation: 169553

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.

Upvotes: 11

Richard Pennington
Richard Pennington

Reputation: 19965

Just give the full name:

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

Upvotes: 1

jschmier
jschmier

Reputation: 15796

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.

Upvotes: 7

Anycorn
Anycorn

Reputation: 51465

link it like you would an object file:

gcc blah.o /usr/local/lib/foo.a -o binary

if you do not like full path, just use a variable. otherwise you could parse LD_Library_Path and test file for existence there

Upvotes: 3

mob
mob

Reputation: 118595

Can you create a symbolic link to stuff.a called libstuff.a? You could even make the symlink in another directory (i.e., not a standard library directory) and use the -L option with gcc to include the directory with the symlink.

Upvotes: 4

Related Questions