Create static library for C program in Linux
Consider your C program has three files, which are addSub.c, mulDiv.c and main.c
addSub.c :
int add(int x, int y)mulDiv.c :
{
return x + y;
}
int sub(int x, int y)
{
return x - y;
}
int mul(int x, int y)main.c :
{
return x * y;
}
int div(int x, int y)
{
return x / y;
}
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%d ", add(10, 15));
printf("%d ", sub(50, 15));
printf("%d ", add(10, 3));
printf("%d ", add(100, 4));
return 0;
}
1 . Compile and create object file for addSub.c and mulDiv.c
gcc -c addSub.c mulDiv.c -Wall2. Create static library( liboperation.a ) for compiled files
ar -cvq libopeartion.a addSub.o mulDiv.o3. We can list the available files from library file using following
ar -t libopeartion.a4. Compile the main program with linking the library
gcc -o output main.c libopeartion.a5. Run the created output executable file
./output
alternative link download