User Tools

Site Tools


Sidebar

projects

cci0 (due 20200123)
pct1 (bonus; due 20200123)
wcp1 (due 20200123)
fwf0 (due 20200129)
pct2 (due 20200129)
wcp2 (due 20200129)
dtr0 (due 20200205)
sof0 (due 20200205)
pct3 (due 20200205)
wcp3 (due 20200205)
dow0 (due 20200212)
pct4 (due 20200212)
wcp4 (due 20200212)
bwp0 (bonus; due 20200226)
pct5 (due 20200226)
wcp5 (due 20200226)
cos0 (due 20200304)
pct6 (due 20200304)
wcp6 (due 20200304)
cbf0 (due 20200311)
pct7 (due 20200311)
wcp7 (due 20200311)
pct8 (due 20200318)
wcp8 (due 20200318)
sam0 (due 20200325)
pct9 (due 20200325)
wcp9 (due 20200325)
afn0 (due 20200401)
pctA (due 20200401)
wcpA (due 20200401)
cnv0 (due 20200422)
pctB (due 20200422)
wcpB (due 20200422)
bwp1 (bonus; due 20200422)
gfo0 (due 20200429)
pctC (due 20200429)
wcpC (due 20200429)
eoce (due 20200513)
haas:spring2020: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/spring2020/cprog/sqrtprog.txt · Last modified: 2013/03/20 15:55 by 127.0.0.1