From what I understood creating a static library is just compiling all the functions independently to obtain object file (*.o).
I recall the "how to" below to see if I have correctly understood the process ....
A first function on a lib
A second function in the lib
Obtaining the object files
Code: Select all
cc -Wall -c FunctionA.c FunctionB.c
Then gather them in Functions.a file with something like
Code: Select all
ar -cvq Functions.a FunctionA.o FunctionB.o
Now if we use the lib in a code like
Code: Select all
void FunctionA(int *);
void FunctionB(int *);
int main()
{
int x;
FunctionA(&x);
return 0;
}
We must link to the lib like that
Code: Select all
cc -o executable-name prog.c Functions.a
In linux, the dynamic libraries are .so files.
It seems to be compiled by special arg with GCC
Something like :
Code: Select all
gcc -Wall -fPIC -c *.c
gcc -shared -Wl,-soname,libctest.so.1 -o libctest.so.1.0 *.o
Considering Linux OS like systems.
The opening of a shared library at runtime is made by "dlopen" and the closing by "dlclose" (
http://man.yolinux.com/cgi-bin/man2html ... and=dlopen).
Code: Select all
#include <stdio.h>
#include <dlfcn.h>
#include "ctest.h"
int main(int argc, char **argv)
{
void *lib_handle;
double (*fn)(int *);
int x;
char *error;
lib_handle = dlopen("/opt/lib/libctest.so", RTLD_LAZY);
if (!lib_handle)
{
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
fn = dlsym(lib_handle, "ctest1");
if ((error = dlerror()) != NULL)
{
fprintf(stderr, "%s\n", error);
exit(1);
}
(*fn)(&x);
printf("Valx=%d\n",x);
dlclose(lib_handle);
return 0;
}
I imagine that there are not such functions available in KOS.
I think that dlopen is near to
Code: Select all
klibrary_t * library_open (const char *name, const char *fn)
and dlclose near to
Code: Select all
int library_close (klibrary_t *lib)
It could be a way of inspiration to solve my needs.
I see in the implementation these comment :
With KOS 1.3.x, we introduce the idea of a loadable library. It's a little
bit different from the standard *n*x concept of a library. Basically in
KOS, a library is a memory image plus a collection of exported names
associated with that image. Each library has a symbolic name and a version
that it can be referenced by, and all "shared libraries" are actually the
same exact library (there is no COW-style private storage per instance). So
it is somewhat similar to an AmigaOS style library or perhaps a Linux
kernel module.
By the way i see in the source code of "library_open" some things about the format of file :
Code: Select all
// Use the ELF functions to load the memory image and extract the
// entry points we need.
if(elf_load(fn, lib, &lib->image) < 0) {
library_destroy(lib);
return NULL;
}
So i ask myself how to generate a file that could be loaded from the "library_open" function ? is that a "canonical" ELF file ?
Finally, seeing the klibrary_t structure that "library_open" function returns, how to execute a function in a loaded library ?
It is a very interesting subject
Thanks for help !