1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
.\"-
.\" Copyright (c) 2024 M. Warner Losh <imp@FreeBSD.org>
.\"
.\" SPDX-License-Identifier: BSD-2-Clause
.\"
.Dd May 9, 2025
.Dt CDEFS 9
.Os
.Sh NAME
.Nm cdefs
.Nd compiler portability macro definitions
.Sh DESCRIPTION
.In sys/cdefs.h
defines macros for compiler, C language standard portability, POSIX standards
compliance and symbol visibility.
It defines programming interfaces for the system header files to adopt to the
many environments
.Fx
supports compilation for.
It defines convenience macros for the
.Fx
sources, tailored to the base
system's needs.
.Pp
Most of these macros are for use inside the
.Fx
sources only.
They are not intended as a general portability layer.
.Sh Supported Compilers
.Bl -tag -offset 2n -width 0
.It Compilers supported for building programs on Fx :
.Bl -column -offset 0n indent-two
.It Sy Compiler Ta Sy Versions
.It gcc Ta 9, 10, 11, 12, 13, 14
.It clang Ta 10, 11, 12, 13, 14, 15, 16, 17, 18
.It TinyC (tcc) Ta 0.9
.It pcc Ta 1.1
.El
.Pp
Due to testing constraints, tcc and pcc may not always work.
.It Compilers supported for building Fx itself:
.Bl -column -offset 0n indent-two
.It Sy Compiler Ta Sy Major Versions Supported
.It gcc Ta 12, 13, 14
.It clang Ta 16, 17, 18
.El
.Pp
Please note: Not every single minor versions of these compilers
will work or are supported.
.El
.Sh Macros and Magic for Programming Environment
.Nm
defines (or refrains from defining) a number of macros to increase portability
of compiled programs.
These are to allow more advanced language features to appear in header files.
The header files assume a compiler that accepts C prototype function
declarations.
They also assume that the compiler accepts ANSI C89 keywords for all language
dialects.
.Ss General Macros
General macros that facilitate multiple language environments and language
dialects.
.Bl -column "---------------"
.It Sy Macro Ta Sy Description
.It Dv __volatile Ta expands to volatile in C++ and C89 and newer environments,
__volatile in pre-ANSI environments that support this extension or nothing
otherwise.
.It Dv __inline Ta expands to inline in C++ and C89 and newer environments,
__inline in pre-ANSI environments that support this extension or nothing
otherwise.
.It Dv __restrict Ta expands to restrict in C99 and newer environments, or
__restrict otherwise.
.It Dv __CONCAT Ta used to paste two pre-processor tokens.
.It Dv __STRING Ta used to convert the argument to a string.
.It Dv __BEGIN_DECLS Ta Start a group of functions.
.It Dv __END_DECLS Ta End a group of functions.
In a C environment, these are defined as nothing.
In a C++ environment, these declare the functions to have
.Dq C
linkage.
.El
.Ss Function, Structure and Variable Modifiers
.Bl -column "---------------"
.It Sy Macro Ta Sy Description
.It Sy __weak_symbol Ta Declare the symbol to be a weak symbol
.It Sy __dead2 Ta Function will not return
.It Sy __pure2 Ta Function has no side effects
.It Sy __unused Ta To Variable may be unused (usually arguments), so do not
warn about it
.It Sy __used Ta Function really is used, so emit it even if it appears unused.
.It Sy __deprecated Ta Function interface has been deprecated, and clients
should migrate to a new interface.
A warning will be issued for clients of this interface.
.It Sy __deprecated1(msg) Ta Function interface has been deprecated, and clients
should migrate to a new interface.
The string
.Fa msg
will be included in a warning issued for clients of this interface.
.It Sy __packed Ta \&Do not have space between structure elements for natural alignment.
Used when communicating with external protocols.
.It Sy __aligned(x) Ta Specify in bytes the minimum alignment for the specified field, structure or variable
.It Sy __section(x) Ta Place function or variable in section Fa x
.It Sy __writeonly Ta Hint that the variable is only assigned to, but do not warn about it.
Useful for macros and other places the eventual use of the result is unknown.
.It Sy __alloc_size(x) Ta The function always returns at least the number of
bytes determined by argument number Fa x
.It Sy __alloc_size2(x,n) Ta The function always returns an array, whose size
is at least the number of bytes determined by argument number Fa x times the
number of elements specified by argument number Fa n
.It Sy __alloc_align(x) Ta Function either returns a pointer aligned to Fa x bytes
or Dv NULL.
.It Sy __min_size Ta Declare the array to have a certain, minimum size
.It Sy __malloc_like Ta Function behaves like the
.Dq malloc
family of functions.
.It Sy __pure Ta Function has no side effects
.It Sy __always_inline Ta Always inline this function when called
.It Sy __fastcall Ta Use the
.Dq fastcall
ABI to call and name mangle this function.
.It Sy __result_use_check Ta Warn if function caller does not use its return value
.It Sy __nodiscard Ta Equivalent to the standard
.Dq [[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
.Vt void ,
or in C++, using an assignment to
.Va 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.
.It Sy __returns_twice Ta Returns multiple times, like
.Xr fork 2
.It Sy __unreachable Ta This code is not reachable at runtime
.It Sy __predict_true(x) Ta Hint to the compiler that
.Fa x
is true most of the time.
Should only be used when performance is improved for a frequently called bit of code.
.It Sy __predict_false(x) Ta Hint to the compiler that
.Fa x
is false most of the time.
Should only be used when performance is improved for a frequently called bit of code.
.It Sy __null_sentinel Ta The varadic function contains a parameter that is
a NULL sentinel to mark the end of its arguments.
.It Sy __exported Ta
.It Sy __hidden Ta
.It Sy __printflike(fmtarg,firstvararg) Ta Function is similar to
.Fn printf
which specifies the format argument with
.Fa fmtarg
and where the arguments formatted by that format start with the
.Fa firstvararg ,
with 0 meaning that
.Dv va_arg
is used and cannot be checked.
.It Sy __scanflike(fmtarg,firstvararg) Ta Function is similar to
.Fn scanf
which specifies the format argument with
.Fa fmtarg
and where the arguments formatted by that format start with the
.Fa firstvararg ,
with 0 meaning that
.Dv va_arg
is used and cannot be checked.
.It Sy __format_arg(f) Ta Specifies that arg
.Fa f
contains a string that will be passed to a function like
.Fn printf
or
.Fa scanf
after being translated in some way.
.It Sy __strfmonlike(fmtarg,firstvararg) Ta Function is similar to
.Fn scanf
which specifies the format argument with
.Fa fmtarg
and where the arguments formatted by that format start with the
.Fa firstvararg ,
with 0 meaning that
.Dv va_arg
is used and cannot be checked.
.It Sy __strtimelike(fmtarg,firstvararg) Ta Function is similar to
.Fn scanf
which specifies the format argument with
.Fa fmtarg
and where the arguments formatted by that format start with the
.Fa firstvararg ,
with 0 meaning that
.Dv va_arg
is used and cannot be checked.
.It Sy __printf0like(fmtarg,firstvararg) Ta Exactly the same
as
.Sy __printflike
except
.Fa fmtarg
may be
.Dv NULL.
.It Sy __strong_reference(sym,aliassym) Ta
.It Sy __weak_reference(sym,alias) Ta
.It Sy __warn_references(sym,msg) Ta
.It Sy __sym_compat(sym,impl,verid) Ta
.It Sy __sym_default(sym,impl,verid) Ta
.It Sy __GLOBAL(sym) Ta
.It Sy __WEAK(sym) Ta
.It Sy __DECONST(type,var) Ta
.It Sy __DEVOLATILE(type,var) Ta
.It Sy __DEQUALIFY(type,var) Ta
.It Sy __RENAME(x) Ta
.It Sy __arg_type_tag Ta
.It Sy __datatype_type_tag Ta
.It Sy __align_up(x,y) Ta
.It Sy __align_down(x,y) Ta
.It Sy __is_aligned(x,y) Ta
.El
.Ss Locking and Debugging Macros
Macros for lock annotation and debugging, as well as some general debugging
macros for address sanitizers.
.Bl -column "---------------"
.It Sy __lock_annotate(x) Ta
.It Sy __lockable Ta
.It Sy __locks_exclusive Ta
.It Sy __locks_shared Ta
.It Sy __trylocks_exclusive Ta
.It Sy __trylocks_shared Ta
.It Sy __unlocks Ta
.It Sy __asserts_exclusive Ta
.It Sy __asserts_shared Ta
.It Sy __requires_exclusive Ta
.It Sy __requires_shared Ta
.It Sy __requires_unlocked Ta
.It Sy __no_lock_analysis Ta
.It Sy __nosanitizeaddress Ta
.It Sy __nosanitizememory Ta
.It Sy __nosanitizethread Ta
.It Sy __nostackprotector Ta
.It Sy __guarded_by(x) Ta
.It Sy __pt_guarded_by(x) Ta
.El
.Ss 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.
.Bl -column "---------------"
.It Sy Keyword Ta Sy Description
.It Sy _Alignas(x) Ta
.It Sy _Alignof(x) Ta
.It Sy _Noreturn Ta Expands to
.Dq [[noreturn]]
in C++-11 and newer compilation environments, otherwise
.Dq __dead2
.It Sy _Static_assert(x, y) Ta Compile time assertion that
.Fa x
is true, otherwise emit
.Fa y
as the error message.
.It Sy _Thread_local Ta Designate variable as thread local storage
.It Sy __generic Ta implement _Generic-like features which aren't entirely possible to emulate the _Generic keyword
.It Sy __noexcept Ta to emulate the C++11 argument-less noexcept form
.It Sy __noexcept_if Ta to emulate the C++11 conditional noexcept form
.It Sy _Nonnull Ta
.It Sy _Nullable Ta
.It Sy _Null_unspecified Ta
.El
.Ss Support Macros
The following macros are defined, or have specific values, to denote certain
things about the build environment.
.Bl -column "---------------"
.It Sy Macro Ta Sy Description
.It Sy __LONG_LONG_SUPPORTED Ta Variables may be declared
.Dq long long .
This is defined for C99 or newer and C++ environments.
.It Sy __STDC_LIMIT_MACROS Ta
.It Sy __STDC_CONSTANT_MACROS Ta
.El
.Ss 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.
.Bl -column "---------------"
.It Sy Macro Ta Sy Description
.It Sy __offsetof(type,field) Ta
.It Sy __rangeof(type,start,end) Ta
.It Sy __containerof(x,s,m) Ta
.El
.Ss ID Strings
This section is deprecated, but is kept around because too much contrib software
still uses these.
.Bl -column "---------------"
.It Sy Macro Ta Sy Description
.It Sy __IDSTRING(name,string) Ta
.It Sy __FBSDID(s) Ta
.It Sy __RCSID(s) Ta
.It Sy __RCSID_SOURCE(s) Ta
.It Sy __SCCSID(s) Ta
.It Sy __COPYRIGHT(s) Ta
.El
.Sh Supported C Environments
.Fx
supports a number C standard environments.
Selection of the language dialect is a compiler-dependent command line option,
though it is usually
.Fl std=XX
where XX is the standard to set for compiling, such as c89 or c23.
.Fx
provides a number of selection macros to control visibility of symbols.
Please see the section on Selection Macros for the specifics.
.Pp
.Bl -tag
.It K \*(Am R
Pre-ANSI Kernighan and Ritchie C.
Sometimes called
.Dq knr
or
.Dq C78
to distinguish it from newer standards.
Support for this compilation environment is dependent on compilers supporting
this configuration.
Most of the old forms of C have been deprecated or removed in
.St -isoC-2023 .
Compilers make compiling in this mode increasingly difficult and support for it
may ultimately be removed from the tree.
.It St -ansiC
.Dv __STDC__
is defined, however
.Dv __STDC_VERSION__
is not.
.Pp
Strict environment selected with
.Dv _ANSI_SOURCE .
.It St -isoC-99
.Dv __STDC_VERSION__ = 199901L
.Pp
Strict environment selected with
.Dv _C99_SOURCE .
.It St -isoC-2011
.Dv __STDC_VERSION__ = 201112L
.Pp
Strict environment selected with
.Dv _C11_SOURCE .
.It ISO/IEC 9899:2018 (“ISO C17”)
.Dv __STDC_VERSION__ = 201710L
.Pp
Strict environment selected with
.Dv _C11_SOURCE
since there are no new C17 only symbols or macros.
.Pp
This version of the standard did not introduce any new features, only made
minor, technical corrections.
.It St -isoC-2023
.Dv __STDC_VERSION__ = 202311L
Strict environment selected with
.Dv _C23_SOURCE
though ISO C23 support is only partially implemented.
.El
.Pp
For more information on C standards, see
.Xr c 7 .
.Ss Programming Environment Selection Macros
Defining the macros outlined 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.
.Bl -column "---------------"
.It Sy Macro Ta Sy Environment
.It Dv _POSIX_SOURCE Ta St -p1003.1-88 including St -ansiC
.It Dv _POSIX_C_SOURCE = 1 Ta St -p1003.1-88 including St -ansiC
.It Dv _POSIX_C_SOURCE = 2 Ta St -p1003.1-90 including St -ansiC
.It Dv _POSIX_C_SOURCE = 199309 Ta St -p1003.1b-93 including St -ansiC
.It Dv _POSIX_C_SOURCE = 199506 Ta St -p1003.1c-95 including St -ansiC
.It Dv _POSIX_C_SOURCE = 200112 Ta St -p1003.1-2001 including St -isoC-99
.It Dv _POSIX_C_SOURCE = 200809 Ta St -p1003.1-2008 including St -isoC-99
.It Dv _POSIX_C_SOURCE = 202405 Ta St -p1003.1-2024 including ISO/IEC 9899:2018 ("ISO C17"),
.It Dv _XOPEN_SOURCE defined Ta St -p1003.1-90 with XPG Extensions to St -susv1 including St -ansiC .
However,
.Fx
implements this as a NOP because too much software breaks with the correct strict environment.
.It Dv _XOPEN_SOURCE = 500 Ta St -p1003.1c-95 and XPG extensions to St -susv2 including St -ansiC
.It Dv _XOPEN_SOURCE = 600 Ta St -p1003.1-2001 and XPG extensions to St -susv3 including St -isoC-99
.It Dv _XOPEN_SOURCE = 700 Ta St -p1003.1-2008 and XPG extensions to St -susv4 including St -isoC-99
.It Dv _XOPEN_SOURCE = 800 Ta St -p1003.1-2024 and XPG extensions to Version 5 of the Single UNIX Specification (“SUSv5”) including ISO/IEC 9899:2018 (“ISO C17”)
.It Dv _ANSI_SOURCE Ta St -ansiC
.It Dv _C99_SOURCE Ta St -isoC-99
.It Dv _C11_SOURCE Ta St -isoC-2011
.It Dv _C23_SOURCE Ta St -isoC-2023
.It Dv _BSD_SOURCE Ta Everything, including Fx extensions
.El
.Pp
Note:
.St -p1003.1-2024
and XPG extensions to Version 5 of the Single UNIX Specification ("SUSv5")
support is incomplete.
.Pp
When both POSIX and C environments are selected, the POSIX environment selects
which C environment is used.
However, when C11 dialect is selected with
.St -p1003.1-2008 ,
definitions for
.St -isoC-2011
are also included.
Likewise, when C23 dialog is selected with
.St -p1003.1-2008
or
.St -p1003.1-2024 ,
definitions for
.St -isoC-2023
are also included.
.Ss Header Visibility Macros
These macros are set by
.Nm
to control the visibility of different standards.
Users must not define these, and doing so will produced undefined results.
They are documented here for developers working on system's header files.
.Bl -column "---------------"
.It Dv __XSI_VISIBLE Ta 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.
.It Dv __POSIX_VISIBLE Ta Make symbols associated with certain standards versions visible.
Set to the value assigned to
.Dv _POSIX_C_SOURCE
by convention with 199009 for
.St -p1003.1-88
and 199209
.St -p1003.1-90 .
.It Dv __ISO_C_VISIBLE Ta The C level that's visible.
Possible values include 1990, 1999, 2011, 2017 and 2023 for
.St -isoC-90 ,
.St -isoC-99 ,
.St -isoC-2011 ,
ISO/IEC 9899:2018 ("ISO C17"), and
.St -isoC-2023 ,
respectively.
.It Dv __BSD_VISIBLE Ta 1 if the
.Fx
extensions are visible, 0 otherwise.
.It Dv __EXT1_VISIBLE Ta 1 if the
.St -isoC-2011
Appendix K 3.7.4.1
extensions are visible, 0 otherwise.
.El
.Sh Supported C++ Environments
.Fx
supports C++11 and newer standards fully.
.Bl -tag
.It ISO/IEC 14882:1998 ("C++98")
.Dv __cplusplus = 199711
.Pp
The first standardized version of C++.
Unlike K \*(Am R support in C, compilers dropped support for versions of
the language prior to C++98.
.It ISO/IEC 14882:2003 ("C++03")
.Dv __cplusplus = 199711
.Pp
Note, this is the same value as C++98.
C++03 did not define a new value for
.Dv __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.
.It ISO/IEC 14882:2011 ("C++11")
.Dv __cplusplus = 201103
.It ISO/IEC 14882:2014 ("C++14")
.Dv __cplusplus = 201402
.It ISO/IEC 14882:2017 ("C++17")
.Dv __cplusplus = 201703
.It ISO/IEC 14882:2020 ("C++20")
.Dv __cplusplus = 202002
.It ISO/IEC 14882:2023 ("C++23")
.Dv __cplusplus = 202302
.El
.Pp
.Fx
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
.Fl pedantic-errors
cannot be reliably enabled for standards older than C++11.
.Sh HISTORY
.In sys/cdefs.h
first appeared in
.Bx 4.3 NET/2 .
|