views:

128

answers:

3

Is there an mechanism or trick to run a function when a program loads?

What I'm trying to achieve...

void foo(void)
{
}

register_function(foo);

but obviously register_function won't run.

so a trick in C++ is to use initialization to make a function run

something like

int throwaway = register_function(foo);

but that doesn't work in C. So I'm looking for a way around this using standard C (nothing platform / compiler specific )

+9  A: 

If you are using GCC, you can do this with a constructor function attribute, eg:

#include <stdio.h>

void foo() __attribute__((constructor));

void foo() {
    printf("Hello, world!\n");
}

int main() { return 0; }

There is no portable way to do this in C, however.

If you don't mind messing with your build system, though, you have more options. For example, you can:

#define CONSTRUCTOR_METHOD(methodname) /* null definition */

CONSTRUCTOR_METHOD(foo)

Now write a build script to search for instances of CONSTRUCTOR_METHOD, and paste a sequence of calls to them into a function in a generated .c file. Invoke the generated function at the start of main().

bdonlan
nice, its what I want, but I was hoping for something more standard C
Keith Nicholas
not doing C++ here, its trivially easy in C++
Keith Nicholas
bdonlan's solution doesn't use C++. Read over it again; it's merely GCC-specific.
Ben Alpert
he changed his answer, it used to have a C++ soloution in it as well
Keith Nicholas
build script is a bit of a hassle as this is part of a framework that should be pretty seamless to integrate into your project. I could do evilness..... I could make a struct (using a macro) and put a magic string and a function pointer in it and then at run time look through memory for the magic string and recover the struct :-)
Keith Nicholas
That's going to be even less portable than using the GCC constructor extension, I think. Moreover, there's a chance of a false positive resulting in a REALLY hard to debug error.
bdonlan
What compilers are you trying to target btw?
bdonlan
ideally, most of the embedded compilers (which are a mish mash of weirdness at the best of times) as well as the more standard MS / GCC compilers
Keith Nicholas
Probably your only hope is to use code generation then... :/
bdonlan
A: 

There is no standard way of doing this although gcc provides a constructor attribute for functions.

The usual way of ensuring some pre-setup has been done (other than a simple variable initialization to a compile time value) is to make sure that all functions requiring that pre-setup. In other words, something like:

static int initialized = 0;
static int x;

int returnX (void) {
    if (!initialized) {
        x = complicatedFunction();
        initialized = 1;
    }
    return x;
}

This is best done in a separate library since it insulates you from the implementation.

paxdiablo
yeah.... however in my case I'm wanting a pre-setup where a function will be registered and stored in a list..... then later on something will go through that list running the registered functions. Currently that means each function needs to be explicitly added in some central location. I was wanting to find a trick where the function can be registered at the point of declaration
Keith Nicholas
+3  A: 

Standard C does not support such an operation. If you don't wish to use compiler specific features to do this, then your next best bet might be to create a global static flag that is initialized to false. Then whenever someone invokes one of your operations that require the function pointer to be registered, you check that flag. If it is false you register the function then set the flag to true. Subsequent calls then won't have to perform the registration. This is similar to the lazy instantiation used in the OO Singleton design pattern.

Amardeep