3 ms·
A great way to handle context-specific headers: common path rooted in context-specific directories coupled with include path management. Useful for platform- a
by matheusmoreira 3y ago
A great way to handle context-specific headers: common path rooted in context-specific directories coupled with include path management.
Useful for platform- and architecture-specific code but can be used for anything really. Anything you want to parameterize and compile conditionally.
Instead of this mess:
#if APP_LINUX
#include <app/linux.h>
#elif APP_BSD
#include <app/bsd.h>
#elif APP_MACOS
#include <app/macos.h>
#else
#error "Unsupported platform"
#endif
Organize things like this instead:
linux/include/app/platform.h
bsd/include/app/platform.h
macos/include/app/platform.h
Then in the makefile:
# Detect it somehow
# or have user provide it
PLATFORM ?= $(shell uname -s)
cc -I include -I $(PLATFORM)/include
Then in the source code:
#include <app/platform.h>
- actionfromafar 3y agoSomething similar in spirit but Meson is what I have settled on. It’s all trade offs all the way down, but it’s what works for me.
- JonChesterfield 3y agoMoving logic from the source code into the compiler invocation is a mixed blessing. In general as the list of -I directories needed to build the thing gets more complicated the ease of building it from something other than the provided build scripts goes down. Much like working things out in the preprocessor is ugly but works with other build systems and over-elaborate configure script style stuff makes for prettier source by moving complexity elsewhere.
- eps 3y agoAmen. That's the way.
- delta_p_delta_x 3y ago+1. I think platform-specific software architecture should be lifted into the build system rather than in the code. This is straightforward with CMake, Meson, Build2, etc. `#ifdef` is a very bad idea and tends to make for leaky abstractions. It litters the code with platform selection and platform-specific behaviour that should ideally be abstracted away, e.g. things like `CreateProcess` vs `fork(); exec();`. With C++20 named modules, this is made even easier, and there is no need to even have an `app/platform.h`. Programmers can literally copy-paste the function declarations into different files, and just select the correct module interface at configure time, and just write import app; Again, most new-ish build systems support this too.