Page 1 of 1

Libraries

Posted: Tue Aug 05, 2003 10:56 am
by HOS
this is a question i have wondered about for a while. how do libraries work? for example, when i write a program for windows or linux i usually start with #include <stdio.h> this is a collection of library functions right? however, isn't it just the prototypes? and does that mean that the actual code for the functions is linked in with the executable? or is the "code" for library functions like printf() just calling a lower-level OS function? and when writing an OS, how do you handle libraries? do both the kernel and stand-alone applications reference libraries, but you just have to write them yourself? do library functions ever call lower-level functions? for example, how does printf() work? does it process the %d's and etc. itself but then call a kernel or lower-level function to actually display the character on the console?

i appreciate any help clearing up my cluttered mind :)

Re:Libraries

Posted: Tue Aug 05, 2003 12:21 pm
by Tim
On Windows, a program links to a .LIB file. That file either provides the functions itself (which get linked inside the program), or redirects to a DLL somewhere.

On Unix, programs link to .a files. That file always provides the functions itself. Alternatively, you can link to a .so file, which will be shared and remain as a separate file.

Linking to a non-shared library (.LIB or .a) is just the same as linking to a normal object file. In fact, static (non-shared) libraries are usually just collections of object files.

Re:Libraries

Posted: Tue Aug 05, 2003 12:36 pm
by anubis
When people write OSes.. they usually write some interrupts and some system calls. They make some advanced libraries based on these basic system calls. Like the Printf could be designed using the interrupt for the display and so on.

These libraries are usually distributed as .a and .lib files which contain the compiled code. The .h are usually given to the developers to know which of the functions r present and to the knowledge of the compiler that these all functions can be got from the library file. U will have to write ur own library or get a readymade source from somewhere like the OSKit project.

Re:Libraries

Posted: Tue Aug 05, 2003 12:39 pm
by HOS
ok, right now i am using djgpp, nasm, and ld. how does one go about creating a .a library? i can create the .c and .h files, but how do you actually compile them all together to a single library file? also, what format is this file?

Re:Libraries

Posted: Tue Aug 05, 2003 12:55 pm
by Pype.Clicker
ar (in package binutils) is the tool used to ARchive .o files into a single library file (.a) ...

the .h file is not joined to the library but is instead distributed separately (usually in the -dev package of the library)

Re:Libraries

Posted: Tue Aug 05, 2003 1:05 pm
by HOS
ok, so once i have created a library, can i simply use its functions in my kernel and applications by linking them with the (ld) parameter -l ? or is there more to it than that?

Re:Libraries

Posted: Tue Aug 05, 2003 1:15 pm
by Tim
That's all you need. Note that you'll need to name a library after any modules that refer to it, not before.

Re:Libraries

Posted: Tue Aug 05, 2003 1:19 pm
by HOS
Note that you'll need to name a library after any modules that refer to it, not before.
what do you mean by this?
also, what format do the .o files need to be in to be able to make a library? can i use assembly functions and make a .o with nasm to include in the library?

thanks

Re:Libraries

Posted: Tue Aug 05, 2003 1:35 pm
by Pype.Clicker
i guess what Tim means is that

Code: Select all

ld kernel.o library.a
is correct, but not

Code: Select all

ld library.a kernel.o
(correct me if i'm wrong)

basically, your .o files must have a format that suits the linker: ELF for a linux environment and COFF for a windows environment. ld --help will usually tell you the supported formats.

Re:Libraries

Posted: Tue Aug 05, 2003 1:40 pm
by HOS
ok, i understand the command line order now.

right now for my asm & c code i am using the a.out object format. i have used it ever since i followed a tutorial that used it a year ago or something. should i be using something like elf instead? and will this work fine with gcc-compiled c mixing with nasm-compiled asm? and what are the advantages/differences i should be aware of if switching to elf?

Re:Libraries

Posted: Tue Aug 12, 2003 7:07 am
by HOS
I tried to create a string library to include in my kernel, but got this warning from gcc:

Code: Select all

In file included from string.c:2:
string.h:5: warning: conflicting types for built-in function `strlen'
i tried using -nostdlib, -nostartfiles, and -nodefaultlibs but i get the same warning every time... i want to be able to write my own library functions, so how can i tell gcc not to look at the standard libraries, or not to look at any "built-in" ones?

thank you

string.h

Code: Select all

#ifndef __STRING_H__
#define __STRING_H__ __STRING_H__

int strlen(char *str);

#endif

string.c

Code: Select all


#include "string.h"

int strlen(char *str)
{
   int retn = 0;
   while (*str++ != 0)
      retn++;
   return retn;
}


Re:Libraries

Posted: Tue Aug 12, 2003 7:18 am
by BI lazy
the option -fno-builtin(or similar look man gcc) will be your friend

Re:Libraries

Posted: Tue Aug 12, 2003 8:18 am
by HOS
-fno-builtin works great. however, now i am unable to get assembly functions in a library to work. i get this error message from gcc:

Code: Select all

undefined reference to `_strfun'
the command i used to compile is:

Code: Select all

gcc main.c hlibc.a
main.c

Code: Select all

#include <stdio.h>
#include "hstring.h"

int main()
{
   char mystr[] = "This is my string.";
   printf(mystr);
   printf(" Length: %d", strlen(mystr));
   printf(" Fun: %d", strfun(1234));
}
string.asm

Code: Select all

global _strfun

_strfun:
   push   ebp
   mov   ebp, esp
   mov   eax, [ebp+4]
   pop   ebp
   ret

this is compiled like this:

Code: Select all

nasmw -f coff string.asm -o string.o
and put into the library like this:

Code: Select all

ar -r hlibc.a string.o
here is the header file hstring.h:

Code: Select all

extern int strfun(int ret);
i know i am doing something wrong because main.c is not seeing strfun()!

Re:Libraries

Posted: Tue Aug 12, 2003 9:43 am
by Pype.Clicker
hmm ... i know that sometimes the linker is sensitive at the return type of the functions ... maybe you should try to put a fake C declaration of strfun with your asm implementation (i mean something like 'fake.c : void f() { strfun();}') that you'd link with your ASM module and see what it gives...

Re:Libraries

Posted: Tue Aug 12, 2003 12:01 pm
by HOS
i have done this, i made a new c file and put a dummy call to strfun(1) inside of it, but i still get the same error message when linking main.c to the library. is my syntax in hstring.h correct for declaring an external asm function? and is the code in string.asm correct for making _strfun global?

i appreciate any feedback!