Hacker News new | ask | show | jobs
by matheusmoreira 1593 days ago
This is really nice. I wonder if there is a way to make this work without constructors.
1 comments

Can possibly be done with a custom section, though not sure that is better and might require fancy compiler flags.
Indeed, I just made a POC with compiler sections: https://github.com/cozzyd/examc

This implementation only works with gcc though probably (it uses the automatic __start_SECTION and __stop_SECTION that gcc generates but clang doesn't seem to... there are likely hacks to make this work anyway though).

In principle, this approach allows interspersing your tests throughout a shared library instead of all in one file (though in that case you wouldn't want the testing function to be called main and you would need a separate driver program for test).