Organizing your source code

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
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

Organizing your source code

Post by justin »

I'm looking for a better way to organize my code. In particular, what is the best way to organize additions to newlib? For example, in order to port cooler things I have had to implement things like termios and dirent. Now I have subdirectories parallel to my 'kernel' subdirectory labeled "syscalls", "termios", and "dirent" which contain code that I have added to my libc. I can foresee this becoming unwieldy. I know many of you have ported things like termios and such and I'm wondering:

1. Where is a good place to put this code?

2. How do you manage things like headers that are used both in the kernel and in user space? For example, things like "struct termios" need to be available to both the kernel and the user. How do you manage this in a way that you don't write the same code twice?

Thanks for your ideas.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Organizing your source code

Post by Gigasoft »

I use the same header for both kernel and user mode code. Others might have a common header file that is included by both header files. Or, you could use conditional compilation to leave out things that are not available in user mode when compiling an user mode program.

I prefer not having a lot of small files in various directories. When things actually become unwieldy, it's usually easy to see how things should be split up. I'd probably put everything related to the standard C library in one or two big files.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Organizing your source code

Post by bluemoon »

I dont care to invent a new schema so I stick to unix style (with little customizations):

general header usable for kernel, ddk and application: /usr/include/
kernel private header: $(KERNEL_PATH)/ and $(KERNEL_PATH)/arch/$(ARCH)
header specific for drivers development: $(KERNEL_PATH)/ddk/
application private header: anything legal

And if there are in fact two different entities for kernel and application, I would call them termios and k_termios to avoid confusing compiler and programmer.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Organizing your source code

Post by Combuster »

Gigasoft wrote:I prefer not having a lot of small files in various directories. When things actually become unwieldy, it's usually easy to see how things should be split up. I'd probably put everything related to the standard C library in one or two big files.
I don't want to have one or two big files, because their total size would be a huge unnavigable mess:
(...)/mos/libc $ find -iname '*.c' | xargs cat | wc -l
13120
And that's only worth half an libc implementation.

Besides, if you do static linking, binary bits are included from an archive on a per-file basis. Which means that all the libc functions you do not use in that app will end up being included in the file size.

as for additions, I only use one separate folder for the host-specific glue per included library - that makes it easy to isolate from the original library code.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply