Skip to content

Common GCC attributes in C

Constructor and Destructor

constructor and destructor attributes are really interesting. The functions registered with constructor attribute is run before main() is executed and functions registered with destructor attribute is run after main() function exits. An interesting use case for these is to pre-load libraries or hook into libraries using LD_PRELOAD.

To see the preload technique, checkout : sheharyaar/byte-sized-programs. The README files has steps to build and run the example. This can be used with both standalone programs and shared libraries.

Here is a simple usage snippet:

#include <stdio.h>
__attribute__((constructor)) int before_main() {
	fprintf(stdout, "executing func %s\n", __FUNCTION__);
	return 0;
}

__attribute__((destructor)) int after_main() {
	fprintf(stdout, "executing func %s\n", __FUNCTION__);
	return 0;
}

int main(int argc, char *argv[]) {
	fprintf(stdout, "executing func %s\n", __FUNCTION__);
	return 0;
}

This will output :

executing func before_main
executing func main
executing func after_main

References

aligned(X)

cleanup

used and unused

noreturn

deprecated