Skip to content

Instantly share code, notes, and snippets.

@Gaurav-Singh-97
Last active December 2, 2016 18:03
Show Gist options
  • Save Gaurav-Singh-97/07c1d4277285e40e7209513c4027cfb1 to your computer and use it in GitHub Desktop.
Save Gaurav-Singh-97/07c1d4277285e40e7209513c4027cfb1 to your computer and use it in GitHub Desktop.
Running functions before and after main() (works on gcc and g++)
/*
Usage :
"test.cpp"
----------
#include <iostream>
#define _START_ begin
#define _EXIT_ end
#include "Startup_Exit.h"
void begin()
{
std::cout << "Running before main()\n";
}
void end()
{
std::cout << "Running after main()\n";
}
int main()
{
std::cout << "Running main\n";
}
*/
#ifndef START_EXIT_H
#define START_EXIT_H
#ifdef __GNUC__
void _START_ (void) __attribute__((constructor));
void _EXIT_ (void) __attribute__((destructor));
#endif
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment