User Tools

Site Tools


Sidebar

projects

wcp1 (due 20220824)
fwf0 (due 20220831)
ntr0 (due 20220831)
pct1 (bonus; due 20220831)
wcp2 (due 20220831)
dtr0 (due 20220907)
pct2 (due 20220907)
pct3 (bonus; due 20220907)
wcp3 (due 20220907)
sof0 (due 20220914)
wcp4 (due 20220914)
pct4 (due 20220915)
dow0 (due 20220921)
wcp5 (due 20220921)
pct5 (bonus; due 20220922)
cnv0 (due 20220928)
gfo0 (due 20220928)
wcp6 (due 20220928)
pct6 (due 20220929)
cnv1 (due 20221005)
pct7 (bonus; due 20221005)
wcp7 (due 20221005)
bwp1 (bonus; due 20221019)
cbf0 (due 20221019)
pct8 (due 20221019)
wcp8 (due 20221019)
pct9 (bonus; due 20221026)
wcp9 (due 20221026)
gfo1 (due 20221102)
pctA (due 20221102)
sam0 (due 20221102)
wcpA (due 20221102)
pctB (bonus; due 20221109)
wcpB (due 20221109)
oop0 (due 20221110)
oop1 (due 20221116)
pctC (due 20221116)
wcpC (due 20221116)
bwp2 (bonus; due 20221201)
oop2 (due 20221201)
pctD (bonus; due 20221201)
wcpD (bonus; due 20221201)
pctE (bonus; due 20221207)
wcpE (bonus; due 20221207)
gfo2 (due 20221208)
EoCE (due 20221219)
haas:fall2022:cprog:sqrtprog

Libraries: libm

The ability to use programming libraries greatly enhances our capabilities, as we can use functions that others have created, instead of having to implement everything ourselves from ground up.

However, with the utilization of libraries, we need to provide additional input to the compiler so that all necessary data bits can be found.

We'll start by going through a simple example- square rooting.

In C, there is a math library (affectionately known as libm) that provides us with a set of math functions. Of them, sqrt() lets us calculate the square root.

Programming Documentation

From the man page (man 3 sqrt), we see the following:

NAME
       sqrt, sqrtf, sqrtl - square root function

SYNOPSIS
       #include <math.h>

       double sqrt(double x);
       float sqrtf(float x);
       long double sqrtl(long double x);

       Link with -lm.

Code

#include <stdio.h>
#include <math.h>
 
int main()
{
    double a = 144, b = 0;
 
    b = sqrt(a);
 
    printf("The square root of %f is %f\n", a, b);
 
    return(0);
}

Compiling, Take One

If we try to compile this code, we should get the following compiler message:

lab46:~/src$ gcc -o math math.c  
/tmp/ccQNybzu.o: In function `main':
math.c:(.text+0x32): undefined reference to `sqrt'
collect2: ld returned 1 exit status
lab46:~/src$ 

Note that there isn't a problem with the source code– that actually compiled cleanly. What it is having a problem with is that it does not know where to find the sqrt() function (because it isn't in the Standard C Library, which is the only library included by default).

Compiling, Take Two

If you recall from the manual page information, we have to link it against the math library (using the -lm option to the compiler). Let's go ahead and do that:

lab46:~/src$ gcc -o math math.c -lm
lab46:~/src$ 

Success!

Verifying it works

At this point we can run it and make sure it works (square root of 144? 12.)

lab46:~/src$ ./math
The square root of 144.000000 is 12.000000
lab46:~/src$ 

Exploration

For those up for a deeper understanding of how things connect together, in your terminal do a cd /lib, followed by an ls/lib is a traditional location for libraries on UNIX systems… here you should be able to find:

  • The C library: libc.so.6
  • The Math library: libm.so.6

Plus, by hunting around in the other “lib” directories (/usr/lib, /usr/local/lib, among others), you can find yet more libraries.

Note that there's a mechanism in place that aids the compiler in finding libraries if they are in these predefined locations. If you're using a library that is located elsewhere, you will need to also use the -L (note the CAPITAL L) option and specify a path.

Conclusion

Basically, to use a library, we need 2 things:

  1. include any pertinent header files in our source code (and of course use said functions correctly)
  2. at compile time, specify -l with the library name (dropping the preceding lib and suffixing so.NUMBER stuff)… so for libm.so.6, it would merely be -lm.
haas/fall2022/cprog/sqrtprog.txt · Last modified: 2013/03/20 15:55 by 127.0.0.1