xref: /haiku/docs/develop/libroot/index.rst (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1The standard C library
2######################
3
4Library organization
5====================
6
7The C library is, unlike in other POSIX systems, called libroot.so. It contains functions typically
8found in libc, libm, libpthread, librt, as well as a few BeOS extensions.
9
10It does not contain functions related to sockets networking, which are instead available in the
11separate libnetwork.so.
12
13POSIX, BSD and GNU extensions
14=============================
15
16overview
17--------
18
19The C library tries to follow the specifications set by the C standard as well as POSIX. These
20provide a complete list of functions that should be implemented in the standard C library. The
21library should not export any other functions, or if it does, they should be in a private namespace,
22for example by prefixing their names with an underscore.
23
24The idea is to avoid symbol name conflicts: applications are allowed to use any function names they
25want, and there is a risk that the standard library accidentally uses the same name.
26
27However, because the standards are a bit conservative, they often don't include functions that
28would be very useful. Historically, other operating systems have provided non-standard extensions
29in their C libraries. In particular, this is common in both glibc and BSD C libraries.
30
31In Haiku, such extensions will be protected by a preprocessor ifdef guard, declared in separate
32headers, and, whenever possible, made available in separate libraries. This allows the main C
33library (libroot) to be more strictly conforming to the C and POSIX standards, while still making
34popular additions available to applications.
35
36features.h
37----------
38
39The file headers/compatibility/bsd/features.h is used to enable the declaration of these additions
40in system header files.
41
42It does so by defining the _DEFAULT_SOURCE variable (for "reasonable defaults"), which is enabled
43if either of the following conditions are satisfied:
44
45- The _GNU_SOURCE or _BSD_SOURCE preprocessor define is defined.
46- The compiler is NOT in strict mode (__STRICT_ANSI__ is not defined) and _POSIX_C_SOURCE is not defined
47
48Typically the following common cases will be used:
49
50- The compiler is run without specific compiler flags: the default in gcc is to enable GNU extensions,
51  so __STRICT_ANSI__ is not defined. As a result, the extended functions are available.
52- The compiler is run with -D_POSIX_C_SOURCE: compiler GNU extensions to C and C++ are enabled,
53  but extended functions are not available.
54- The compiler is set to use a strict standard (for example -std=c11). In this case, __STRICT_ANSI__
55  is defined, and the extended functions are not available.
56- The compiler is set to use a strict standard, but either _BSD_SOURCE or _GNU_SOURCE is defined.
57  In this case, the C and C++ language extensions are disabled, but the extra functions are available.
58
59header files organization
60-------------------------
61
62In addition to the _DEFAULT_SOURCE guard, the nonstandard functions are declared in separate headers,
63found in headers/compatibility/bsd and headers/compatibility/gnu, depending on where the function
64was first introduced.
65
66These directories are inserted before the standard ones (such as headers/posix). Since the extensions
67are usually added in existing headers, these headers are overridden in these directories.
68
69The overriden header uses #include_next (a gcc extension) to include the original one. It then
70defines any extensions available.
71
72There is a problem with this: #include_next is itself a nonstandard feature. So, in order to use a
73fully standard compiler which would not recognize it, the compatibility headers directories should
74be removed from the include paths.
75
76libgnu and libbsd
77-----------------
78
79Moving the declarations out of the system headers is fine, but it is not enough. The function
80definitions should also be moved out of libroot. So they are implemented in separate libraries,
81libgnu and libbsd. Applications using these extended functions will need to link with these.
82
83weak symbols
84------------
85
86In some cases, the code can't easily be moved out of libroot. There are various cases where this
87can happen, for example:
88
89- Other code in libroot depends on the nonstandard function
90- The functions was in libroot in BeOS and can't be removed without breaking BeOS apps
91
92In these cases, the function will be provided with an underscore at the start of its name. This
93moves it to a namespace where libroot is allowed to define anything it needs. Then, a non-prefixed
94version can be exported as a weak symbol. This allows applications to define their own version of
95the function, since the one in libroot is "weak", the application one will be used instead for the
96application code. Since code in libroot references the underscore-prefixed function only, it will
97not be affected by this, and will still call the libroot-provided function.
98
99This creates extra complexity, so, whenever possible, the functions should be moved to separate
100libraries instead.
101
102BeOS and Haiku specific functions
103=================================
104
105In addition to the standard C library, libroot also contains functions specific to BeOS and Haiku.
106Unfortunately, no precautions were taken with these, and they can conflict with application defined
107symbols.
108
109We can't change this for symbols existing from BeOS, as it would break binary compatibility. We
110may need to change this in R2. However, we should keep this in mind when adding new functions to
111libroot. There is no well-defined solution for this currently, but the strategies documented above
112can be used if needed.
113