CDEFS(9)

πŸ“¦ version 15.1, in freebsd 15.1, nixpkgs unstable

CDEFS(9) β€” compiler portability macro definitions πŸ’…

okay so real talk: this is the CDEFS(9) page from the Kernel Developer's Manual, and it's basically the group chat of <sys/cdefs.h>. bestie, this header is doing so much β€” compiler portability, C language standard portability, POSIX standards compliance, symbol visibility, all of it. it defines the programming interfaces that system header files use to adapt to the many environments FreeBSD supports compilation for, and it also defines convenience macros for the FreeBSD sources, tailored to the base system's needs. ✨

🫢 heads up bestie: most of these macros are for use inside the FreeBSD sources only. they are not intended as a general portability layer, no cap.

Supported Compilers 🧚

compilers we support for building programs on FreeBSD

  • gcc β€” versions 9, 10, 11, 12, 13, 14
  • clang β€” versions 10, 11, 12, 13, 14, 15, 16, 17, 18
  • TinyC (tcc) β€” version 0.9
  • pcc β€” version 1.1
😭 okay so due to testing constraints, tcc and pcc may not always work. they're on the list, they just might ghost you on a bad day.

compilers we support for building FreeBSD itself

  • gcc β€” major versions 12, 13, 14
  • clang β€” major versions 16, 17, 18
πŸ’€ please note: not every single minor version of these compilers will work or is supported. iykyk.

Macros and Magic for Programming Environment πŸŽ€

cdefs defines (or refrains from defining, king of boundaries) a number of macros to increase portability of compiled programs. the whole point is letting more advanced language features show up in header files. these header files assume a compiler that accepts C prototype function declarations, and they also assume the compiler accepts ANSI C89 keywords for all language dialects. periodt.

General Macros ✨

general macros that facilitate multiple language environments and language dialects.

  • __volatile β€” expands to volatile in C++ and C89 and newer environments, to __volatile in pre-ANSI environments that support this extension, or nothing otherwise.
  • __inline β€” expands to inline in C++ and C89 and newer environments, to __inline in pre-ANSI environments that support this extension, or nothing otherwise.
  • __restrict β€” expands to restrict in C99 and newer environments, or __restrict otherwise.
  • __CONCAT β€” used to paste two pre-processor tokens.
  • __STRING β€” used to convert the argument to a string.
  • __BEGIN_DECLS β€” start a group of functions.
  • __END_DECLS β€” end a group of functions. in a C environment these are defined as nothing 🌸 in a C++ environment, these declare the functions to have β€œC” linkage.

Function, Structure and Variable Modifiers πŸ’…

  • __weak_symbol β€” declare the symbol to be a weak symbol.
  • __dead2 β€” function will not return.
  • __pure2 β€” function has no side effects.
  • __unused β€” variable may be unused (usually arguments), so don't warn about it.
  • __used β€” function really is used, so emit it even if it appears unused.
  • __deprecated β€” function interface has been deprecated, and clients should migrate to a new interface. a warning will be issued for clients of this interface.
  • __deprecated1(msg) β€” function interface has been deprecated, and clients should migrate to a new interface. the string msg will be included in a warning issued for clients of this interface.
  • __packed β€” do not have space between structure elements for natural alignment. used when communicating with external protocols.
  • __aligned(x) β€” specify in bytes the minimum alignment for the specified field, structure or variable.
  • __section(x) β€” place function or variable in section x.
  • __writeonly β€” hint that the variable is only assigned to, but don't warn about it. useful for macros and other places where the eventual use of the result is unknown.
  • __alloc_size(x) β€” the function always returns at least the number of bytes determined by argument number x.
  • __alloc_size2(x,n) β€” the function always returns an array, whose size is at least the number of bytes determined by argument number x times the number of elements specified by argument number n.
  • __alloc_align(x) β€” function either returns a pointer aligned to x bytes or NULL.
  • __min_size β€” declare the array to have a certain, minimum size.
  • __malloc_like β€” function behaves like the β€œmalloc” family of functions.
  • __pure β€” function has no side effects.
  • __always_inline β€” always inline this function when called.
  • __fastcall β€” use the β€œfastcall” ABI to call and name mangle this function.
  • __result_use_check β€” warn if function caller does not use its return value.
  • __nodiscard β€” equivalent to the standard β€œ[[nodiscard]]” attribute. if applied to a function, warn if function caller does not use its return value. the warning may be silenced using a cast to void, or in C++, using an assignment to std::ignore. if applied to a struct, C++ class or enum, this applies to all functions returning values of that type. if applied to a C++ constructor, this applies to creating instances of the class using that constructor.
  • __returns_twice β€” returns multiple times, like fork(2).
  • __unreachable β€” this code is not reachable at runtime.
  • __predict_true(x) β€” hint to the compiler that x is true most of the time. should only be used when performance is improved for a frequently called bit of code.
  • __predict_false(x) β€” hint to the compiler that x is false most of the time. should only be used when performance is improved for a frequently called bit of code.
  • __null_sentinel β€” the varadic function contains a parameter that is a NULL sentinel to mark the end of its arguments.
  • __exported β€” (no description on this page, it's just sitting here being iconic)
  • __hidden β€” (no description on this page either, lowkey mysterious πŸ•΅οΈ)
  • __printflike(fmtarg,firstvararg) β€” function is similar to printf() which specifies the format argument with fmtarg and where the arguments formatted by that format start with the firstvararg, with 0 meaning that va_arg is used and cannot be checked.
  • __scanflike(fmtarg,firstvararg) β€” function is similar to scanf() which specifies the format argument with fmtarg and where the arguments formatted by that format start with the firstvararg, with 0 meaning that va_arg is used and cannot be checked.
  • __format_arg(f) β€” specifies that arg f contains a string that will be passed to a function like printf() or scanf() after being translated in some way.
  • __strfmonlike(fmtarg,firstvararg) β€” function is similar to scanf() which specifies the format argument with fmtarg and where the arguments formatted by that format start with the firstvararg, with 0 meaning that va_arg is used and cannot be checked.
  • __strtimelike(fmtarg,firstvararg) β€” function is similar to scanf() which specifies the format argument with fmtarg and where the arguments formatted by that format start with the firstvararg, with 0 meaning that va_arg is used and cannot be checked.
  • __printf0like(fmtarg,firstvararg) β€” exactly the same as __printflike except fmtarg may be NULL.
  • __strong_reference(sym,aliassym) β€” (no description on this page)
  • __weak_reference(sym,alias) β€” (no description on this page)
  • __warn_references(sym,msg) β€” (no description on this page)
  • __sym_compat(sym,impl,verid) β€” (no description on this page)
  • __sym_default(sym,impl,verid) β€” (no description on this page)
  • __GLOBAL(sym) β€” (no description on this page)
  • __WEAK(sym) β€” (no description on this page)
  • __DECONST(type,var) β€” (no description on this page)
  • __DEVOLATILE(type,var) β€” (no description on this page)
  • __DEQUALIFY(type,var) β€” (no description on this page)
  • __RENAME(x) β€” (no description on this page)
  • __arg_type_tag β€” (no description on this page)
  • __datatype_type_tag β€” (no description on this page)
  • __align_up(x,y) β€” (no description on this page)
  • __align_down(x,y) β€” (no description on this page)
  • __is_aligned(x,y) β€” (no description on this page)

Locking and Debugging Macros πŸ”₯

macros for lock annotation and debugging, plus some general debugging macros for address sanitizers. ngl this section is a whole mood board of names with no descriptions attached on this page, so here they all are, verbatim and unbothered:

  • __lock_annotate(x)
  • __lockable
  • __locks_exclusive
  • __locks_shared
  • __trylocks_exclusive
  • __trylocks_shared
  • __unlocks
  • __asserts_exclusive
  • __asserts_shared
  • __requires_exclusive
  • __requires_shared
  • __requires_unlocked
  • __no_lock_analysis
  • __nosanitizeaddress
  • __nosanitizememory
  • __nosanitizethread
  • __nostackprotector
  • __guarded_by(x)
  • __pt_guarded_by(x)

Emulated Keywords 🌈

as C evolves, many of the old macros we once used have been incorporated into the standard language. as this happens, we add support for these keywords as macros for older compilation environments. sometimes this results in a nop in the older environment β€” cute, harmless, slaying nothing. πŸ’«

  • _Alignas(x) β€” (no description on this page)
  • _Alignof(x) β€” (no description on this page)
  • _Noreturn β€” expands to β€œ[[noreturn]]” in C++-11 and newer compilation environments, otherwise β€œ__dead2”.
  • _Static_assert(x, y) β€” compile time assertion that x is true, otherwise emit y as the error message.
  • _Thread_local β€” designate variable as thread local storage.
  • __generic β€” implement _Generic-like features which aren't entirely possible to emulate the _Generic keyword.
  • __noexcept β€” to emulate the C++11 argument-less noexcept form.
  • __noexcept_if β€” to emulate the C++11 conditional noexcept form.
  • _Nonnull β€” (no description on this page)
  • _Nullable β€” (no description on this page)
  • _Null_unspecified β€” (no description on this page)

Support Macros πŸ¦‹

the following macros are defined, or have specific values, to denote certain things about the build environment.

  • __LONG_LONG_SUPPORTED β€” variables may be declared β€œlong long”. this is defined for C99 or newer and C++ environments.
  • __STDC_LIMIT_MACROS β€” (no description on this page)
  • __STDC_CONSTANT_MACROS β€” (no description on this page)

Convenience Macros πŸ“

these macros make it easier to do a number of things, even though strictly speaking the standard places their normal form in another header. so convenient, so thoughtful, we stan. πŸ’–

  • __offsetof(type,field) β€” (no description on this page)
  • __rangeof(type,start,end) β€” (no description on this page)
  • __containerof(x,s,m) β€” (no description on this page)

ID Strings πŸ‘‘

πŸ™ˆ this section is deprecated, but it's kept around because too much contrib software still uses these. yes it's giving legacy, but we don't abandon our friends.
  • __IDSTRING(name,string) β€” (no description on this page)
  • __FBSDID(s) β€” (no description on this page)
  • __RCSID(s) β€” (no description on this page)
  • __RCSID_SOURCE(s) β€” (no description on this page)
  • __SCCSID(s) β€” (no description on this page)
  • __COPYRIGHT(s) β€” (no description on this page)

✨ Supported C Environments ✨

okay so FreeBSD supports a whole bunch of C standard environments, and bestie, picking which one is a compiler-dependent command line option β€” usually it's -std=XX where XX is the standard you're compiling for, like c89 or c23. FreeBSD also tosses you a bunch of selection macros to control symbol visibility. specifics go in the Selection Macros section, iykyk.

  • K & R β€” Pre-ANSI Kernighan and Ritchie C. Sometimes called "knr" or "C78" to tell it apart from the newer standards. Whether you can actually use this depends on your compiler supporting it. Most of the old forms of C have been deprecated or removed in . Compilers make compiling in this mode increasingly difficult and support for it may ultimately be removed from the tree. 😬
  • ANSI X3.159-1989 ("ANSI C89") β€” __STDC__ is defined, however __STDC_VERSION__ is not. Strict environment selected with _ANSI_SOURCE.
  • ISO/IEC 9899:1999 ("ISO C99") β€” __STDC_VERSION__ = 199901L. Strict environment selected with _C99_SOURCE.
  • ISO/IEC 9899:2011 ("ISO C11") β€” __STDC_VERSION__ = 201112L. Strict environment selected with _C11_SOURCE.
  • ISO/IEC 9899:2018 ("ISO C17") β€” __STDC_VERSION__ = 201710L. Strict environment selected with _C11_SOURCE since there are no new C17 only symbols or macros. This version of the standard did not introduce any new features, only made minor, technical corrections. πŸ’…
  • __STDC_VERSION__ = 202311L Strict environment selected with _C23_SOURCE though ISO C23 support is only partially implemented. πŸ™ˆ

For more info on C standards, go peep c(7).


πŸŽ€ Programming Environment Selection Macros πŸŽ€

Defining the macros down below requests that the system header files provide only the functions, structures and macros (symbols) defined by the appropriate standard, while suppressing all extensions. However, system headers not defined by that standard may define extensions. You may only define one of the following for any compilation unit. one. single. one. periodt.

  • _POSIX_SOURCE β€” IEEE Std 1003.1-1988 ("POSIX.1") including ANSI X3.159-1989 ("ANSI C89")
  • _POSIX_C_SOURCE = 1 β€” IEEE Std 1003.1-1988 ("POSIX.1") including ANSI X3.159-1989 ("ANSI C89")
  • _POSIX_C_SOURCE = 2 β€” ISO/IEC 9945-1:1990 ("POSIX.1") including ANSI X3.159-1989 ("ANSI C89")
  • _POSIX_C_SOURCE = 199309 β€” IEEE Std 1003.1b-1993 ("POSIX.1") including ANSI X3.159-1989 ("ANSI C89")
  • _POSIX_C_SOURCE = 199506 β€” IEEE Std 1003.1c-1995 ("POSIX.1") including ANSI X3.159-1989 ("ANSI C89")
  • _POSIX_C_SOURCE = 200112 β€” IEEE Std 1003.1-2001 ("POSIX.1") including ISO/IEC 9899:1999 ("ISO C99")
  • _POSIX_C_SOURCE = 200809 β€” IEEE Std 1003.1-2008 ("POSIX.1") including ISO/IEC 9899:1999 ("ISO C99")
  • _POSIX_C_SOURCE = 202405 β€” including ISO/IEC 9899:2018 ("ISO C17"),
  • _XOPEN_SOURCE defined β€” ISO/IEC 9945-1:1990 ("POSIX.1") with XPG Extensions to Version 1 of the Single UNIX Specification ("SUSv1") including ANSI X3.159-1989 ("ANSI C89"). However, FreeBSD implements this as a NOP because too much software breaks with the correct strict environment. 😭 iconic behaviour tbh.
  • _XOPEN_SOURCE = 500 β€” IEEE Std 1003.1c-1995 ("POSIX.1") and XPG extensions to Version 2 of the Single UNIX Specification ("SUSv2") including ANSI X3.159-1989 ("ANSI C89")
  • _XOPEN_SOURCE = 600 β€” IEEE Std 1003.1-2001 ("POSIX.1") and XPG extensions to Version 3 of the Single UNIX Specification ("SUSv3") including ISO/IEC 9899:1999 ("ISO C99")
  • _XOPEN_SOURCE = 700 β€” IEEE Std 1003.1-2008 ("POSIX.1") and XPG extensions to Version 4 of the Single UNIX Specification ("SUSv4") including ISO/IEC 9899:1999 ("ISO C99")
  • _XOPEN_SOURCE = 800 β€” and XPG extensions to Version 5 of the Single UNIX Specification ("SUSv5") including ISO/IEC 9899:2018 ("ISO C17")
  • _ANSI_SOURCE β€” ANSI X3.159-1989 ("ANSI C89")
  • _C99_SOURCE β€” ISO/IEC 9899:1999 ("ISO C99")
  • _C11_SOURCE β€” ISO/IEC 9899:2011 ("ISO C11")
  • _C23_SOURCE
  • _BSD_SOURCE β€” Everything, including FreeBSD extensions. the whole buffet, no crumbs left behind πŸ“
Note: and XPG extensions to Version 5 of the Single UNIX Specification ("SUSv5") support is incomplete. ⚠️

When both POSIX and C environments are selected, the POSIX environment selects which C environment is used. However, when C11 dialect is selected with IEEE Std 1003.1-2008 ("POSIX.1"), definitions for ISO/IEC 9899:2011 ("ISO C11") are also included. Likewise, when C23 dialog is selected with IEEE Std 1003.1-2008 ("POSIX.1") or , definitions for are also included.


πŸ‘€ Header Visibility Macros πŸ‘€

These macros are set by cdefs to control the visibility of different standards. Users must not define these, and doing so will produced undefined results. They're documented here for developers working on system's header files. so like, look but don't touch, bestie 🫢

  • __XSI_VISIBLE β€” Restricts the visibility of XOPEN Single Unix Standard version. Possible values are 500, 600, 700 or 800, corresponding to Issue 5, 6, 7, or 8 of the Single Unix Standard. These are extra functions in addition to the normal POSIX ones.
  • __POSIX_VISIBLE β€” Make symbols associated with certain standards versions visible. Set to the value assigned to _POSIX_C_SOURCE by convention with 199009 for IEEE Std 1003.1-1988 ("POSIX.1") and 199209 ISO/IEC 9945-1:1990 ("POSIX.1").
  • __ISO_C_VISIBLE β€” The C level that's visible. Possible values include 1990, 1999, 2011, 2017 and 2023 for ISO/IEC 9899:1990 ("ISO C90"), ISO/IEC 9899:1999 ("ISO C99"), ISO/IEC 9899:2011 ("ISO C11"), ISO/IEC 9899:2018 ("ISO C17"), and , respectively.
  • __BSD_VISIBLE β€” 1 if the FreeBSD extensions are visible, 0 otherwise.
  • __EXT1_VISIBLE β€” 1 if the ISO/IEC 9899:2011 ("ISO C11") Appendix K 3.7.4.1 extensions are visible, 0 otherwise.

πŸ¦‹ Supported C++ Environments πŸ¦‹

FreeBSD supports C++11 and newer standards fully. slay.

  • ISO/IEC 14882:1998 ("C++98") β€” __cplusplus = 199711. The first standardized version of C++. Unlike K & R support in C, compilers dropped support for versions of the language prior to C++98. πŸ’€
  • ISO/IEC 14882:2003 ("C++03") β€” __cplusplus = 199711. Note, this is the same value as C++98. C++03 did not define a new value for __cplusplus. There is no way, at compile time, to detect the difference. The standard resolved a number of defect reports and slightly expanded value initialization. Most compilers support it the same as C++98. 😭 cannot believe they did that to us.
  • ISO/IEC 14882:2011 ("C++11") β€” __cplusplus = 201103
  • ISO/IEC 14882:2014 ("C++14") β€” __cplusplus = 201402
  • ISO/IEC 14882:2017 ("C++17") β€” __cplusplus = 201703
  • ISO/IEC 14882:2020 ("C++20") β€” __cplusplus = 202002
  • ISO/IEC 14882:2023 ("C++23") β€” __cplusplus = 202302

FreeBSD uses llvm project's libc++. However, they are removing support for C++ prior to C++11. While programs can still build with earlier environments for now, these changes mean that -pedantic-errors cannot be reliably enabled for standards older than C++11. ⚠️


πŸ“œ HISTORY πŸ“œ

<sys/cdefs.h> first appeared in 4.3BSD NET/2.


and that's the whole page bestie, you ate it πŸ’…βœ¨ this lil' edition is from Debian and it's dated May 9, 2025 β€” go forth and compile responsibly, mwah πŸŽ€πŸ¦‹πŸ’–

πŸ“¦ where this page comes from