This is a small tutorial that introduces how to create a small C library.
This is a small tutorial about how to create a small C library.
First we create the header file "lbi.h":
void func_a(); float func_b(int, float); char *func_c(int *, int);
And the implementation "lbi.c":
#include <stdio.h>
#include "lbi.h"
void func_a()
{
printf("Hello, func_a\n");
}
float func_b(int a, float b)
{
return (((float)a) + b);
}
char *func_c(int *ar, int n)
{
int i;
char *s = "Hello, func_c";
for (i = 0; i < n; i++)
{
printf("ar[%d]: %d\n", i, ar[i]);
}
return s;
}
Using gcc, we can compile this small lib with:
gcc -c lbi.c gcc -shared -o liblbi.so lbi.o
This produces the small lib liblbi.so that we can link
with -llbi.
You can add more functions to it for your experimentations.
Compile with the parameter -llbi to link with the lib:
liblbi.so.
Now that we have liblbi.so we can test with gcc
in the console.
We can define LD_LIBRARY_PATH so that our example lib will
be found correctly:
$ export LD_LIBRARY_PATH=$(pwd) $ echo $LD_LIBRARY_PATH /tmp/lib-tut
arrays:
TODO
Arrays of char's:
TODO
Compile with the -static parameter to create a static
library.
Static libraries usually have a
.a file extension.
TODO
TODO
2023-06-29.b
© 2019 2021 2022 2023 Florent Monnier
You can use this document according to the terms of either one of these licenses:
You can reuse the pieces of code according to the terms of either one of these licenses: