3 ms·
Can you give an example of a use case where this is needed?
by qppo 7y ago
Can you give an example of a use case where this is needed?
- codys 7y agoOne place it's used is to avoid central "registration" for multiple "things". For example, consider a program with many fairly separate "subcommands". Using this mechanism allows adding a new subcommand simply by linking in a new source file (without changing a shared source file to list the new command).
- berti 7y agoI use this idea in embedded work, where the code is split into "modules". The same firmware source is shared amongst various differing pieces of hardware, each of which utilise a different subset of modules.
- clort 7y agoThis is used for constructor & destructor functions in C code. The function pointers get put in an array in a certain section which is collated then the startup and exit code calls them. Ordering is not guaranteed.
- saagarjha 7y agoNote that the ordering that the constructors and destructors will be run once the binary has been constructed is guaranteed by your C runtime.
- jhallenworld 7y agoI like to use this technique for CLI command and help tables. It eliminates having a single giant table in a file, instead you have a C macro which makes the system aware of your command right next to your command definition. int my_command(..) { ... } COMMAND(my_command, "command_name", "example command") HELP("use --foo argument for this...") HELP("use --bar argument for that...") Also used this all the time back when I programmed assembly language. It's definitely a problem that this is not standardized. For example, TI has their own C compiler for their ARM CPUs, not everybody uses gcc..