Associating a C Library with a Custom Name

I use gcc to compile the program that I need to associate with the C library with a non-standard name; it is called stuff.a instead of libstuff.a .

I canโ€™t change the file name (permission problems).

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 execute a (dynamic) link?

EDIT:

Thanks to everyone, and my apologies for the poorly worded question.

I didnโ€™t even have a common object (I thought I could dynamically link the * .a file), so this confused many of you. Again, apologies for my ignorance.

What I ended up with was creating a shared object in the local directory by adding it to my LD_LIBRARY_PATH environment variable and linking again.

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

Thanks again.

+7
c gcc linker
source share
6 answers

Assuming that the public version of the static library does not exist, you might need to create one. Remember that the static library stuff.a is just an archive.

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

This assumes that you want to link to it as a shared library, and not just compile it into your binary.

+6
source share

You should take a look at the gcc manual :

The only difference between using -l and specifying the file name is that -l surrounds the library with 'lib' and '.a' and searches for multiple directories.

There is nothing wrong with using the argument to stuff.a .

+5
source share

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

+4
source share

bind it the same way as with the object file:

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

if you don't like the full path, just use a variable. otherwise you could parse the LD_Library_Path and the test file to exist there

+3
source share

Just provide the full name:

 gcc program.c /path/to/library/stuff.a -oprogram 
+1
source share

I know that the problem turned out to be one of the attempts to set the static library as dynamically linked, but I wanted to add this for posterity:

If you pass the argument to the -l parameter with a colon, : , it will treat the name as a literal, not the name requiring the addition of "lib" to the front, and the file extension will be added to the end,

In the case where you specify where you want to link yourself to static.a , not libstatic.a , and assuming that you intend to link to it statically, the following will work:

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

Of course, you could do as Richard Pennington and Anicorn mentioned, and just include the fully corrected library in the command line:

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

All Articles