CDEFS(9)
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β versions9,10,11,12,13,14clangβ versions10,11,12,13,14,15,16,17,18- TinyC (
tcc) β version0.9 pccβ version1.1
π okay so due to testing constraints,tccandpccmay 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 versions12,13,14clangβ major versions16,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 tovolatilein C++ and C89 and newer environments, to__volatilein pre-ANSI environments that support this extension, or nothing otherwise.__inlineβ expands toinlinein C++ and C89 and newer environments, to__inlinein pre-ANSI environments that support this extension, or nothing otherwise.__restrictβ expands torestrictin C99 and newer environments, or__restrictotherwise.__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 stringmsgwill 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 orNULL.__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 tovoid, or in C++, using an assignment tostd::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, likefork(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 toprintf()which specifies the format argument withfmtargand where the arguments formatted by that format start with thefirstvararg, with0meaning thatva_argis used and cannot be checked.__scanflike(fmtarg,firstvararg)β function is similar toscanf()which specifies the format argument withfmtargand where the arguments formatted by that format start with thefirstvararg, with0meaning thatva_argis used and cannot be checked.__format_arg(f)β specifies that arg f contains a string that will be passed to a function likeprintf()orscanf()after being translated in some way.__strfmonlike(fmtarg,firstvararg)β function is similar toscanf()which specifies the format argument withfmtargand where the arguments formatted by that format start with thefirstvararg, with0meaning thatva_argis used and cannot be checked.__strtimelike(fmtarg,firstvararg)β function is similar toscanf()which specifies the format argument withfmtargand where the arguments formatted by that format start with thefirstvararg, with0meaning thatva_argis used and cannot be checked.__printf0like(fmtarg,firstvararg)β exactly the same as__printflikeexceptfmtargmay beNULL.__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_Generickeyword.__noexceptβ to emulate the C++11 argument-lessnoexceptform.__noexcept_ifβ to emulate the C++11 conditionalnoexceptform._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_SOURCEsince 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__ = 202311LStrict environment selected with_C23_SOURCEthough 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_SOURCEdefined β 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 are500,600,700or800, 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_SOURCEby convention with199009for IEEE Std 1003.1-1988 ("POSIX.1") and199209ISO/IEC 9945-1:1990 ("POSIX.1").__ISO_C_VISIBLEβ The C level that's visible. Possible values include1990,1999,2011,2017and2023for 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β1if the FreeBSD extensions are visible,0otherwise.__EXT1_VISIBLEβ1if the ISO/IEC 9899:2011 ("ISO C11") Appendix K 3.7.4.1 extensions are visible,0otherwise.
π¦ 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
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- π the page's license: BSD-2-Clause
- Copyright (c) 2024 M. Warner Losh <imp@FreeBSD.org>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms