7

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.

3
  • Does a shared object version of the library exist (stuff.so)? Commented Jan 21, 2010 at 19:09
  • forgot to mention, I am working on Linux. Commented Jan 21, 2010 at 19:09
  • @jschmier: no, only the .a library. Commented Jan 21, 2010 at 19:14

6 Answers 6

11

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.

Sign up to request clarification or add additional context in comments.

2 Comments

There was a "(dynamic)" in the original question but I am less and less sure the OP really meant it.
Thank you. I was not aware of this, and with yours and other comments now I am able to solve my problem.
7

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.

3 Comments

Interesting, but the .o files must have been compiled as position-independent code for this to work, which they may not have been if they were intended for a static library, right?
This is the right answer because I did not even have a shared object. I ended up creating one, and linking. From an executable of 1.3M (with the original approach) I passed to an executable of 5.8K. This is awesome. Thank you.
+1 for advanced crystal ball usage or mind reading capabilities ;)
6

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

1 Comment

I like this answer as it demonstrates the connection of -L and -l and the colon trick.
4

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.

2 Comments

I was thinking of doing this, and it may be what I end up doing. I just wondered if there is a special flag I can pass to gcc to let it know that the name is non-standard.
Yes, I tested a symbolic link works. Be aware also of the LD_LIBRARY_PATH environment variable.
3

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

2 Comments

Thank you unkown, I did not now this.
you cannot link to the static library dynamically, it must be compile time static linkage. shared objects must be compiled with PIC, static libraries are typically not compiled like that. you still have to load shared library into memory at runtime, unless you are running multiple threads, you will not gain much
1

Just give the full name:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.