Mbed compiles and links a lot of unnecessary files

I guess this is a known problem of Mbed and there is a new build system being developed as mentioned in [MBed CLI Alternative] mbed-cmake. In the meantime you can use the bare-metal profile, where you have to indicate which components you want to use explicitly in mbed_app.json, otherwise they don’t get compiled. Downside is of course that you cannot use the RTOS.

BTW, the mbed_config.h is generated after the components are selected, i.e. it does not enable/disable anything. It is just an artifact in this context.