Make freestanding and hosted LIBC

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Make freestanding and hosted LIBC

Post 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 :)
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Make freestanding and hosted LIBC

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Make freestanding and hosted LIBC

Post 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.
Every good solution is obvious once you've found it.
afritieefy
Posts: 12
Joined: Fri Dec 02, 2011 6:13 am

Re: Make freestanding and hosted LIBC

Post 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 :)
Post Reply