C
Christof Warlich
Hi,
I'm working on a (template) library that is up to now entirely
implemented in header-files. This makes the library quite convenient to
use as no extra object code needs to be linked when using the library.
But now, the library needs to run some initialization code at system
startup. Usually, this initialization code would be called from a .cc
file once at system startup, e.g. by assigning the init function to a
global variable:
// init.cc
#include <stdio.h>
inline int init() {
// whatever needs to be initialized goes here
printf("initializing\n");
return 0;
}
static int dummy = init();
With this simple approach, calling init() cannot be done from a header
file, as it will be called as often as it will be included by .cc files.
Thus, it is required to link the application with the object derived
from init.cc now.
To avoid this, I replaced the above with the following code, now having
everything defined in a header file:
// init.h
#include <stdio.h>
inline int init() {
// whatever needs to be initialized goes here
printf("initializing\n");
return 0;
}
// ensure that init is called exactly once
inline int initialize(void) {
static int dummy = init();
return 0;
}
static int dummy = initialize();
This works as desired: With the two sample application files one.cc and
two.cc:
// one.cc
#include "init.h"
int main(void) {
return 0;
}
// two.cc
#include "init.h"
and compilation:
$ g++ -I. one.cc two.cc
the init() function is only called once:
$ ./a.out
initializing
The only concern is that a new global dummy variable will be created for
every application file that includes init.h. Any ideas to avoid this?
Christof
I'm working on a (template) library that is up to now entirely
implemented in header-files. This makes the library quite convenient to
use as no extra object code needs to be linked when using the library.
But now, the library needs to run some initialization code at system
startup. Usually, this initialization code would be called from a .cc
file once at system startup, e.g. by assigning the init function to a
global variable:
// init.cc
#include <stdio.h>
inline int init() {
// whatever needs to be initialized goes here
printf("initializing\n");
return 0;
}
static int dummy = init();
With this simple approach, calling init() cannot be done from a header
file, as it will be called as often as it will be included by .cc files.
Thus, it is required to link the application with the object derived
from init.cc now.
To avoid this, I replaced the above with the following code, now having
everything defined in a header file:
// init.h
#include <stdio.h>
inline int init() {
// whatever needs to be initialized goes here
printf("initializing\n");
return 0;
}
// ensure that init is called exactly once
inline int initialize(void) {
static int dummy = init();
return 0;
}
static int dummy = initialize();
This works as desired: With the two sample application files one.cc and
two.cc:
// one.cc
#include "init.h"
int main(void) {
return 0;
}
// two.cc
#include "init.h"
and compilation:
$ g++ -I. one.cc two.cc
the init() function is only called once:
$ ./a.out
initializing
The only concern is that a new global dummy variable will be created for
every application file that includes init.h. Any ideas to avoid this?
Christof