As whiteflags mentioned, you can use your IDE. However for a quickstart on doing this with gcc, try the following with your example:
1. Make a header file for your library called squared.h. Example:
Code:
#ifndef SQUARED_H
#define SQUARED_H
float squared(float s_input);
#endif
2. Make a simple tester (user program) for your library. I will call it test_squared.c. The tester must include squared.h. Example:
Code:
#include <stdio.h>
#include "squared.h"
int main()
{
printf("The result of squared(4.0f) is %.8f\n", squared(4.0f));
return 0;
}
3. Make an implementation of your library. I will call it squared.c. The implementation must include squared.h. Example:
Code:
#include "squared.h"
#include <math.h>
float squared(float s_input)
{
return powf(s_input, 2.0f);
}
4. Compile the implementation into an .o file. Notice I am linking with libm because the squared.c implementation in turn uses the libm library. I will call it squared.o. Example command:
gcc -Wall -std=gnu99 -c -o squared.o squared.c -lm
5. Create an archive file to hold the object file(s). The GNU Naming convention is that the archive file must begin with "lib" and end with ".a". You can put as many object files as you want into the archive. I will call your archive libsquared.a. Example command:
ar r libsquared.a squared.o
6. Compile the tester and specify that you want to link to your libsquared library. The switch "-L." is needed if you have the archive file in the current directory. Without that switch, gcc will only look for libraries in the standard places. Example command:
gcc -L. -Wall -std=gnu99 -o test_squared test_squared.c -lsquared