Basic example of simple C program include static library compilation-source code
install gcc compiler:
apt-get install gcc
All following steps are done in the same directory !! NO need to have an root permissions.
create library1 and save it as liblibrary_1.c:
#include
void library_1(char *val)
{
printf("Passed value to library_1: %s\n", val);
}
create library2 and save it as liblibrary_2.c:
#include
void library_2(char *val)
{
printf("Passed value to library_2: %s\n", val);
}
Compile both c libraries source codes:
gcc -c liblibrary_1.c liblibrary_2.c
NOTE: make sure that you include new line after your code or you will receive message:
liblibrary_1.c:5:2: warning: no newline at end of file
liblibrary_2.c:5:2: warning: no newline at end of file
create static library from both library objects with ar command:
ar cvr libarchive.a liblibrary_1.o liblibrary_2.o
make table of contents:
ranlib libarchive.a
write header file and save it as header_file.h:
/* Header File */
void library_1(char *);
void library_2(char *);
write main program file and save it as program.c:
#include
#include "header_file.h"
int main()
{
library_1("Hello from library_1 !");
library_2("Hello from library_2 !");
exit(0);
}
create a object of main program.c:
gcc -c program.c
compile main program from object and include main library “libarchive.a” which includes both, library1 and library2 objects:
gcc -o program program.o libarchive.a
run the program:
./program
output:
Passed vaule to library_1: Hello from library_1 !
Passed vaule to library_2: Hello from library_2 !
Filed under: C, Linux, Linux Programming Tutorial, Programming