Page 1 of 1

Make freestanding and hosted LIBC

Posted: Fri Dec 02, 2011 6:39 am
by afritieefy
Hi! :)
I'm quite new to OS Development, but my kernel already supports monotasking! Until now, everything went fine. But...
I want to make 2 versions of my C library: a hosted version, and a freestanding version. Using the preprocessor variable __STDC_HOSTED__ I can throw an error when a systemcall is called in the freestanding version, like:

Code: Select all

void syscall()
{
#if __STDC_HOSTED__ == 1
...
#else
#error System calls not allowed in kernel!
#endif
}
Here is my original makefile. The problem is that I don't know how to make two "targets": libc.a (with __STDC_HOSTED 1) and libk.a (with __STDC_HOSTED 0) . Anyone knows a good solution for this?

Code: Select all

CC=tcc -Wall -I../include
OBJS=$(patsubst %.c,%.o,$(wildcard *.c))

all : libc.a

.c.o:
	$(CC) -D__STDC_HOSTED__=1 -c -o$@ $<

# Rules
libc.a: $(OBJS)
	tiny_libmaker.exe libc.a $(OBJS)
Thanks in advance!

PS: sorry for my bad english :)

Re: Make freestanding and hosted LIBC

Posted: Fri Dec 02, 2011 6:48 am
by bluemoon
How about re-structure your source code layout:

Code: Select all

src
|- foo.c
|- bar.c
|- freestand
   |- syscall.c
|-myos
   |- syscall.c
And have make build different targets with different set of files, while avoid compiles the same code multiple times with different flags - that way mess up dependency checking.

Re: Make freestanding and hosted LIBC

Posted: Fri Dec 02, 2011 6:55 am
by Solar
+1 bluemoon.

Also, __STDC_HOSTED__ is defined by the standard, to be set automatically by the compiler. You should name your symbol used in this approach differently, to avoid confusion.


@bluemoon:


Dependency tracking with different flags is perfectly possible, just adds a bit complexity to the Makefile. The Makefile tutorial hints at a way to do just that.

Re: Make freestanding and hosted LIBC

Posted: Fri Dec 02, 2011 7:29 am
by afritieefy
@Solar: I'm using Tiny C Compiler (TCC) which doesn't define __STDC_HOSTED__, so I have to define it myself (with value 0 or 1).
@Bluemoon: I'll give that approach a try :)