Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to initialize static variable by calling function

Tags:

c

Is this possible?

static bool initialize()
{
  TRC_SCOPE_INIT(...); 
  ...
}

static bool initialized = initialize();

To make a very long story short, I need to call a series of macros (to initialize debugging messages) as early as possible (before thread X is started, and I don't have the ability to know when thread X is started).

like image 906
Scott Siddall Avatar asked May 21 '12 01:05

Scott Siddall


1 Answers

If you're using GCC (or clang), you can use __attribute__((constructor)):

static bool initialized = false;

__attribute__((constructor))
static void initialize(void) {
    initialized = true;
    // do some other initialization
}

int main(int argc, char **argv) {
    // initialize will have been run before main started
    return 0;
}
like image 87
icktoofay Avatar answered Sep 22 '22 10:09

icktoofay