26

I know that these are some common/basic libraries, but what do they mean exactly?

For example, I know, that -lm is some math library, but is this the standard math library or what?

-lz for compression? What compression?

And I don't have any idea what -lrt is.


What are these things?

  • math library. Is it the same that we use when we include <cmath> or <math.h>?
  • compress library. What does this mean? Does it provide some tools that we can use to compress files, or does it help the compiler/linker to do some compress things?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
  • Answering your second point, the compression library provides routines which you can use in your application. Let's say you have a buffer of data which you would like to apply some compression to before sending over the wire - this library provides an OS, patent unencumbered compressions algorithm to do that. – Nim Apr 14 '11 at 12:41
  • 1
    The canonical may be *[Why do you have to link the math library in C](https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c)*? – Peter Mortensen Oct 27 '22 at 23:57

5 Answers5

29
Nim
  • 33,299
  • 2
  • 62
  • 101
6

The switch -lX generally means to load the library libX.so.

libm is the standard math library; it contains sin(), cos(), atanh(), all that good stuff.

libz is Zlib, a compression library which can do gzip, deflate, and a few other formats.

There are a couple of different librt's out there: one is the POSIX realtime extensions; another is a library of general-purpose programming aids.

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
4

-lz links to the zlib, -lm to the math and -lrt to the realtime extensions library.

Axel Gneiting
  • 5,293
  • 25
  • 30
  • The last link is (effectively) broken: *"Gone. The requested resource is no longer available on this server and there is no forwarding address. Please remove all references to this resource."* – Peter Mortensen Oct 29 '22 at 22:44
  • The second link seems to be broken (times out). A general problem with sub domain *sources.redhat.com* (*redhat.com* works OK)? – Peter Mortensen Oct 29 '22 at 22:53
2

The previous answers are all correct. The one thing I would add, being a C novice myself, is that the -l argument tells the compiler to link your code with some library.

The confusion for me and probably others is that there is no space when calling the -l plus the name of the lib. so -lz, you are linking to the "z".

Note that these libraries are installed in your system. Either they came with the distribution you are using, or you installed using a package manager or compiled from source (make, make install ...).

Since those are very basic (and old) library APIs, they have very short names. As you progress and install specific libraries in your system, you see more verbose names tagging the -l there.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

All of them are standard C as far as I know, probably included in libstdc++ (your question is tagged C++).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110