Is there a standard related to how a c library should be implemented or do you have to relearn peculiarity of a library implementations when you switch to new board which provides a different BSP?
First off, the C standard defines something called a "freestanding" implementation, as opposed to a "hosted" implementation (which is what most of us are familiar with, the full range of C functions supported by the underlying OS).
A "freestanding" implementation needs to define only a subset of the C library headers, namely those that do not require support, or even the definition of functions (they merely do
When you're taking the next step toward a hosted implementation, you will find that there are only very few functions that really need to interface "the system" in any way, with the rest of the library being implementable on top of those "primitives". In implementing the PDCLib, I made some effort to isolate them in a separate subdirectory for easy identification when porting the lib to a new platform (examples for the Linux port in parenthesis):
extern char * * environ)
time() (not implemented yet)
<stdio.h> (arguably the most "OS-involved" of the C99 headers):
- some way to open a file (
- some way to close it (
- some way to remove it (
- some way to rename it (
- some way to write to it (
- some way to read from it (
- some way to reposition within it (
Certain details of the library are optional, with the standard merely offering them to be implemented in a standard way but not making such an implementation a requirement.
time() function may legally just return
(time_t)-1 if no time-keeping mechanics are available.
The signal handlers described for
<signal.h> need not be invoked by anything other than a call to
raise(), there is no requirement that the system actually sends something like
SIGSEGV to the application.
The C11 header
<threads.h>, which is (for obvious reasons) very dependent on the OS, need not be provided at all if the implementation defines
There are more examples, but I don't have them at hand right now.
The rest of the library can be implemented without any help from the environment.(*)
(*)Caveat: The PDCLib implementation is not complete yet, so I might have overlooked a thing or two. ;-)