12 days ago · Tech · 0 comments

I like to write C that is portable to build as well as run. To that end, all of my projects should build with any C toolchain (compiler cc, linker ld, and archiver ar) that supports a standard for C99, C11 or C23. They have no other development dependencies: no need for pkgconfig, or the presence of headers or libraries beyond those standard from libc (on Unix systems a few of the standard Unix headers not in the C standard are used).I test my projects with gcc, clang, tcc, cproc, and cl.exe—and most will even build with chibicc (a toy compiler that has some issues). In this article I go over the methods I use.Table of ContentsCompiler specific builtinsGraphics and other librariesMathCompiler specific builtinsIf you want to take advantage of compiler builtins, check they are available. E.g. the following code uses an assert macro based on __builtin_unreachable from gcc/clang so long as it is available.#ifndef __has_builtin #define __has_builtin(unused) 0 #endif #if !defined(assert)…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.