17

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?

Maxpm
  • 24,113
  • 33
  • 111
  • 170
  • 1
    It was always my understanding that this is a naming convention that is *not* enforced by the compiler toolchain. – Rafe Kettler Jun 03 '11 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`. – eduffy Jun 03 '11 at 18:35

1 Answers1

29

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.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Out of curiosity, is the absence of a space after `-l` optional? It seems rather inconsistent. – Maxpm Jun 03 '11 at 18:38
  • 4
    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. –  Jun 03 '11 at 18:39
  • @Max Don't use a space. I've seen it work and not work, but in general both -l, -L, and -I expect the path/name to come next, with no intervening space. –  Jun 03 '11 at 18:39
  • 11
    @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 Norum Jun 03 '11 at 18:40
  • 2
    @Carl: Especially, if you do *not* have the liberty to name the library like you want to. :) – Xeo Jun 03 '11 at 18:45
  • @Neil Those old Unix guys really _knew_ how to use the keyboard... on a 300 baud terminal! – n. m. could be an AI Jun 03 '11 at 18:50
  • 1
    @n.m. Yeah, I've used one of those in fact this one http://www.columbia.edu/cu/computinghistory/la36.html, but I still think in this case they took things a little too far. –  Jun 03 '11 at 18:53
  • In fact DEC's TOPS-10 OS user interface (including the SOS editor) was perfectly (though noisily) usable on a DecWriter, with no minimalist library (or other command) names. –  Jun 03 '11 at 19:01
  • 1
    @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. – Mark Ransom Jun 03 '11 at 20:04
  • @Mark Even I have never come across a 110 baud line. Are you claiming you have? If so, connecting what to what? –  Jun 03 '11 at 20:23
  • @Neil before video displays were cheap and plentiful the common interface was a Teletype: http://en.wikipedia.org/wiki/ASR-33_Teletype I had the pleasure of using one with both an HP minicomputer and a Control Data mainframe. P.S. I've used the Decwriter as well, it was a big step up. – Mark Ransom Jun 03 '11 at 20:34
  • 1
    @Mark OK, now we must wait for the 75 baud guy, who also lives in a hole in the motorway and walked uphill to school, both ways. Hey, it's my Dad - at least WRT the hole and the uphill both ways! –  Jun 03 '11 at 20:40
  • @Mark haha, have you ever used one of these - http://www.columbia.edu/cu/computinghistory/026.html (OK, perhaps not that exact model, but not far off) - one of my strangest assignments was to write a simulator for a card punch machine on an IBM PC, so that the card punch operators could continue plying their skills when the actual hardware was discontinued. –  Jun 03 '11 at 20:47
  • The vital "surrounds ..." documentation is missing from `man gcc` nowadays. I'm using GCC 11.3.0 with the Nix package manager. – Steve Chavez Jul 02 '23 at 22:24