I’ll now walk you through all of the required steps to compile the library to an object file and to then use this library in a test program. To make things easier, this is all demonstrated on the next few pages via our Linux server coding windows. But you could just as easily follow all of the described steps by logging onto the server. You’d first have to save the files complexlib.c, complexlib.h and testComplex.c in one directory on the server.
Here is a quick program to test some functionality of our library. You’ll have to work on the server in order to compile the library and link it to the test program. I’ll walk you through this below. Add more tests here!
/* Program to test our complex number library
*
* testComplex.c
*
* compile this program to an object file using
* gcc -c -o testComplex.o testComplex.c
*
* then link this object file with the complex number library
* gcc -o testComplex testComplex.o complexlib.o -lm
*
*/
#include <stdio.h>
#include "complexlib.h"
int main(void) {
complex_t z,w;
z = complex(2,3);
w = complex(-4,-6.5);
display_c(z);
printf(" + ");
display_c(w);
printf(" = ");
display_c(add_c(z,w));
printf("\n");
return(0);
}