18

From http://www.adp-gmbh.ch/cpp/gcc/create_lib.html:

Note: the library must start with the three letters lib and have the suffix .a.

Is this an operating system convention, or a gcc/ar quirk? Xcode seems to be able to create libraries without the prefix. What's it doing differently?

2
  • 1
    It was always my understanding that this is a naming convention that is not enforced by the compiler toolchain. Commented Jun 3, 2011 at 18:35
  • 6
    probably using the full path to the archive. The lib/.a convection is used with the -l flag, ie. -lXXX looks for libXXX.a or libXXX.so. Commented Jun 3, 2011 at 18:35

1 Answer 1

31

You can name a library whatever you want, but if you want gcc's -l flag to find the right one, you need to name it the way that link describes. For example:

gcc -o myapp myapp.c -lm 

Will compile myapp.c, link the resulting object with libm.a, and output an executable called myapp. These days, there might be a more complicated search path involving dynamic library names, etc., but you should get the basic idea from this example.

From the gcc man page:

-l library ...

... surrounds library with lib and .a and searches several directories.

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

14 Comments

Those old unix guys really hated using the keyboard! This is one feature of the unix-style linker that I've never really seen the justification for being continued into the present. Of course, you can always provide the full path and name of the library.
@Max - yeah it's optional. You can put a space there. But when you link against libiberty.a, it's kind of fun to write -liberty...
@Carl: Especially, if you do not have the liberty to name the library like you want to. :)
@n.m. Yeah, I've used one of those in fact this one columbia.edu/cu/computinghistory/la36.html, but I still think in this case they took things a little too far.
@n.m. You had 300 baud? You lucky dog, be glad you weren't stuck with 110! They don't call those ports tty for nothing.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.