views:

143

answers:

5

Hello,

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.

+4  A: 

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.

mobrule
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.
Arrieta
+4  A: 

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.

jschmier
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?
Pascal Cuoq
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.
Arrieta
+1 for advanced crystal ball usage or mind reading capabilities ;)
Christoph
+3  A: 

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

aaa
As I had originally written, I don't want to do this because it increases the size of the executable.
Arrieta
@Arrieta: not according to the gcc manual
Christoph
Thank you unkown, I did not now this.
Arrieta
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
aaa
+1  A: 

Just give the full name:

gcc program.c /path/to/library/stuff.a -oprogram
Richard Pennington
+4  A: 

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.

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