UNIFDEF(1)
π UNIFDEF(1) β like, the manual page, but make it make sense π
okay bestie, gather round, because this is the Programmer's Manual entry for unifdef (and its chaotic sibling unifdefall), and it is genuinely iconic. no cap. let's get into it. π¦
β¨ NAME β¨
unifdef, unifdefall β remove preprocessor conditionals from code. that's the whole job. it's giving surgical. π
β¨ SYNOPSIS β¨
unifdef [-bBcdehKkmnsStV] [-Ipath] [-[i]Dsym[=val]] [-[i]Usym] ...
[-f defile] [-x {012}] [-M backext] [-o outfile] [infile ...]
unifdefall [-Ipath] ... file
like look at all those flags. she's got range. π
β¨ DESCRIPTION β¨
so unifdef selectively processes conditional cpp(1) directives. it removes from a file both the directives and any additional text that they say should be removed β while otherwise leaving the file completely alone. lowkey respectful of your code. π«Ά
unifdef acts on #if, #ifdef, #ifndef, #elif, #else, and #endif lines, using macros specified in -D and -U command line options or in -f definitions files. a directive gets processed if the macro specs are sufficient to give a definite value for its control expression.
- if the result is false, the directive and the following lines under its control are removed. bye. π
- if the result is true, only the directive itself is removed.
- an
#ifdefor#ifndefdirective gets passed through unchanged if its controlling macro isn't specified. - any
#ifor#elifcontrol expression that has an unknown value, or thatunifdefcan't parse, gets passed through unchanged.
by default, unifdef ignores #if and #elif lines with constant expressions β but you can tell it to process them by specifying the -k flag on the command line. π
π§ the expression syntax, explained π§
it understands a commonly-used subset of the expression syntax for #if and #elif lines:
- integer constants
- integer values of macros defined on the command line
- the
defined()operator - the operators
!,~,-(unary),*,/,%,+,-,<,<=,>,>=,==,!=,&,^,|,&&,|| - parenthesized expressions
division by zero is treated as an unknown value. no, it will not yell at you. it just... doesn't know. π
a kind of βshort circuitβ evaluation is used for the && operator: if either operand is definitely false then the result is false, even if the value of the other operand is unknown. similarly, if either operand of || is definitely true then the result is true. it's doing the absolute most to be helpful. β¨
when evaluating an expression, unifdef does not expand macros first. the value of a macro must be a simple number, not an expression. a limited form of indirection is allowed, where one macro's value is the name of another. πͺ
π object-like vs function-like macros π
in most cases, unifdef does not distinguish between object-like macros (without arguments) and function-like macros (with arguments). a function-like macro invocation can appear in #if and #elif control expressions.
- if the macro isn't explicitly defined, or is defined with the
-Dflag on the command-line, or with#definein a-fdefinitions file, its arguments are ignored. - if a macro is explicitly undefined on the command line with the
-Uflag, or with#undefin a-fdefinitions file, it may not have any arguments since this leads to a syntax error. β οΈ
π΅οΈ what it knows about C π΅οΈ
unifdef understands just enough about C to know when one of the directives is inactive because it's inside a comment, or can't be evaluated because it's split by a backslash-continued line. it spots unusually-formatted preprocessor directives and passes them through unchanged when the layout is too odd for it to handle. (see the βBUGSβ section below.) it's not nosy, it's just observant. πΈ
π unifdefall π
a script called unifdefall can be used to remove all conditional cpp(1) directives from a file. it uses unifdef -s and cpp -dM to get lists of all the controlling macros and their definitions (or lack thereof), then invokes unifdef with appropriate arguments to process the file. she's a delegator. she's a project manager. π
β¨ OPTIONS β¨
-Dsym=valβ specify that a macro is defined to a given value.-Dsymβ specify that a macro is defined to the value1.-Usymβ specify that a macro is undefined.
if the same macro appears in more than one argument, the last occurrence dominates. last one wins, like always. π
-iDsym[=val]-iUsymβ C strings, comments, and line continuations are ignored within#ifdefand#ifndefblocks controlled by macros specified with these options.-fdefile β the file defile contains#defineand#undefpreprocessor directives, which have the same effect as the corresponding-Dand-Ucommand-line arguments. you can have multiple-farguments and mix them with-Dand-Uarguments; later options override earlier ones.
each directive must be on a single line. object-like macro definitions (without arguments) are set to the given value. function-like macro definitions (with arguments) are treated as if they are set to 1.
Warning: string literals and character constants are not parsed correctly in -f files. π
-bβ replace removed lines with blank lines instead of deleting them. mutually exclusive with the-Boption.-Bβ compress blank lines around a deleted section. mutually exclusive with the-boption.-cβ complement, i.e., lines that would have been removed or blanked are retained and vice versa. flip it. reverse it. π-dβ turn on printing of debugging messages.-eβ by default,unifdefwill report an error if it needs to remove a preprocessor directive that spans more than one line, for example, if it has a multi-line comment hanging off its right hand end. the-eflag makes it ignore the line instead.-hβ print help.-Ipath β specifies tounifdefallan additional place to look for#includefiles. this option is ignored byunifdeffor compatibility withcpp(1)and to simplify the implementation ofunifdefall.-Kβ always treat the result of&&and||operators as unknown if either operand is unknown, instead of short-circuiting when unknown operands can't affect the result. this option is for compatibility with older versions ofunifdef.-kβ process#ifand#eliflines with constant expressions. by default, sections controlled by such lines are passed through unchanged because they typically start#if 0and are used as a kind of comment to sketch out future or past development. it would be rude to strip them out, just as it would be for normal comments. π₯Ί-mβ modify one or more input files in place. if an input file is not modified, the original is preserved instead of being overwritten with an identical copy.-Mbackext β modify input files in place, and keep backups of the original files by appending the backext to the input filenames. a zero length backext behaves the same as the-moption.-nβ add#linedirectives to the output following any deleted lines, so that errors produced when compiling the output file correspond to line numbers in the input file.-ooutfile β write output to the file outfile instead of the standard output when processing a single file.-sβ instead of processing an input file as usual, this option causesunifdefto produce a list of macros that are used in preprocessor directive controlling expressions.-Sβ like the-soption, but the nesting depth of each macro is also printed. this is useful for working out the number of possible combinations of interdependent defined/undefined macros.-tβ disables parsing for C strings, comments, and line continuations, which is useful for plain text. this is a blanket version of the-iDand-iUflags.-Vβ print version details.-x {012}β set exit status mode to zero, one, or two. see the βEXIT STATUSβ section below for details.
unifdef takes its input from stdin if there are no file arguments. you must use the -m or -M options if there are multiple input files. you can specify input from stdin or output to stdout with -. π
unifdef works nicely with the -Dsym option of diff(1). besties fr fr. π«Ά
β¨ EXIT STATUS β¨
in normal usage, unifdef's exit status depends on the mode set using the -x option.
- if the exit mode is zero (the default) then
unifdefexits with status0if the output is an exact copy of the input, or with status1if the output differs. - if the exit mode is one,
unifdefexits with status1if the output is unmodified or0if it differs. yes it's inverted. it's quirky like that. π - if the exit mode is two,
unifdefexits with status zero in both cases.
in all exit modes, unifdef exits with status 2 if there is an error. π
the exit status is 0 if the -h or -V command line options are given.
β¨ DIAGNOSTICS β¨
the messages it can hit you with, verbatim, because these are iconic:
EOF in comment
Inappropriate #elif, #else or #endif
Missing macro name in #define or #undef
Obfuscated preprocessor control line
Premature EOF (with the line number of the most recent unterminated #if)
Too many levels of nesting
Unrecognized preprocessor directive
Unterminated char or string literal
β¨ SEE ALSO β¨
the unifdef home page is http://dotat.at/prog/unifdef π
β¨ HISTORY β¨
the unifdef command appeared in 2.9BSD. ANSI C support was added in FreeBSD 4.7. she's a legacy. she's a veteran. she's been here. β
β¨ AUTHORS β¨
the original implementation was written by Dave Yost <Dave@Yost.com>. Tony Finch <dot@dotat.at> rewrote it to support ANSI C. two icons, one tool. π
β¨ BUGS β¨
okay so here's the honest part, bestie. she's not perfect. nobody is.
- Expression evaluation is very limited.
- Character constants are not evaluated. String literals and character constants in
-fdefinition files are ignored rather than parsed as part of a macro's replacement tokens. - Only the basic form of C++ raw string literals is recognized, like
R"(string)"without delimiters as inR"delimiter(string)delimiter". - Source files are processed one line at a time, so preprocessor directives split across more than one physical line (because of comments or backslash-newline) cannot be handled in every situation.
- Trigraphs are not recognized.
- There is no support for macros with different definitions at different points in the source file.
- The text-mode and ignore functionality does not correspond to modern
cpp(1)behaviour.
please send bug reports by email to β¨dot@dotat.atβ©. she reads her inbox. π
and that's the whole page, bestie!! this was UNIFDEF(1) from the Programmer's Manual, dated December 3, 2015, and honestly? she ate. go forth and strip those conditionals. periodt. π
β¨ππ¦πΈ
π¦ where this page comes from
- alpine 3.24: unifdef-doc 2.12-r4 Β· BSD-2-Clause AND BSD-3-Clause
- arch rolling: unifdef 2.12-4 Β· GPL
- debian trixie: unifdef 2.12-1
- epel 10: unifdef 2.12-5.el10_1 Β· BSD-2-Clause AND BSD-3-Clause
- fedora 43: unifdef 2.12-6.fc43 Β· BSD-2-Clause AND BSD-3-Clause
- fedora 44: unifdef 2.12-7.fc44 Β· BSD-2-Clause AND BSD-3-Clause
- nixos 26.05: unifdef 2.12
- nixpkgs unstable: unifdef 2.12
- opensuse-leap 16.0: unifdef 2.12-160000.2.2 Β· BSD-2-Clause
- opensuse-leap 16.0: unifdef 2.12-160000.2.2 Β· BSD-2-Clause
- opensuse-leap 16.0: unifdef 2.12-160000.2.2 Β· BSD-2-Clause
- opensuse-leap 16.0: unifdef 2.12-160000.2.2 Β· BSD-2-Clause
- ubuntu noble: unifdef 2.12-1
- ubuntu resolute: unifdef 2.12-1
- ubuntu stonking: unifdef 2.12-1.1ubuntu1
- Copyright (c) 1985, 1991, 1993 The Regents of the University of California. All rights reserved
- Copyright (c) 2002 - 2015 Tony Finch <dot@dotat.at>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms