Am 18.11.2026 um 03:48 schrieb Story:
> Hello misc@. How do you choose your compiler flags? Do you do it by 
> project, or do you have a big block that you use every time? Do you have
> a generic Make file that you copy as a template, or do you hand pick
> the compiler specifics every single time you write new code?
> 
> Or, do you, like me, just hit the up button on the terminal until you 
> get to the very long gcc command with a ton of arguments and edit the 
> file arguments?
> 
> 
> thank you for your attention to this trivial matter,
> 
> Story Lynn from Kentucky

Use the build system and the project defaults. Do not hand-roll
long compiler command lines for real work.

Base system / your own code in the OpenBSD style

The standard approach is a short Makefile that includes the
system fragments:

PROG=   foo
SRCS=   foo.c bar.c
# optional:
# COPTS+= -DSOMETHING
# MAN=    foo.1
.include <bsd.prog.mk>

Defaults come from /usr/share/mk/sys.mk (and friends):

CFLAGS ?= -O2 ${PIPE} ${DEBUG}
PIPE ?= -pipe
CXXFLAGS is set similarly.

Additional control knobs you actually use:

COPTS / CXXOPTS - extra flags appended to CFLAGS/CXXFLAGS.
DEBUG - adds -g (and changes stripping behavior).
WARNINGS=yes - adds the usual CDIAGFLAGS (-Wall -Wpointer-arith
-Wstrict-prototypes -Wmissing-prototypes ...).

See bsd.prog.mk, bsd.lib.mk, bsd.own.mk, and bsd.README for the
full set of variables. This is what the tree itself uses.

Ports

The ports tree does the same thing via bsd.port.mk. User
overrides normally go in /etc/mk.conf (or on the make command
line) as COPTS, CXXOPTS, etc. Many ports hard-code or ignore
some flags, so the framework appends COPTS where it can. Stick
to the framework; do not invent a parallel build system.

What not to do

Hitting up-arrow until you find the giant cc/clang line is fine
for a 30-second test. It is not how anyone maintains code. The
project has repeatedly said (on misc@ and elsewhere) that custom
flag combinations (-march=native, aggressive -O3, random
security/optimization mixes, etc.) are not supported. The
developers only chase bugs that appear with the defaults used in
release builds. Deviating is a good way to create unique bugs
that no one else can reproduce.

OpenBSD's clang (on most architectures) already enables a number
of things by default that you would otherwise have to add by
hand: PIE, stack protector, retguard on supported arches,
-fwrapv, etc. See clang-local(1).

Practical advice for a newbie

1. Start with the tiny Makefile above and the stock flags.
2. Turn on WARNINGS=yes while developing.
3. Only add COPTS when you have a concrete reason (a needed -D,
   a warning you must silence after understanding it, etc.).
4. If you are writing something that belongs in base or ports,
   follow the existing style of the directory you are working in.
5. Read style(9) and the relevant bsd.*.mk man pages / README.

That is the boring, reliable answer. The long hand-crafted
compiler lines are for temporary experiments, not for anything
you expect to maintain.

Reply via email to