This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: `initializer element is not constant' issue
Aiee :)
Hello!
On Wed, Aug 22, 2001 at 12:15:59PM +0200, Gigi Sullivan wrote:
> On Wed, Aug 22, 2001 at 04:29:48AM -0300, Alexandre Oliva wrote:
> > > `initializer element is not constant' error from the compiler.
> >
> > > Is there a way to work around this, please? [1]
> >
> > I'm afraid initializers of global variables cannot contain function
> > calls in C.
>
> Is it definetly just impossible?
Ok, I found something that could be acceptable to me, a sorta
of workaround (even, oviously, is not just what I wanted to
do ;))
Any comments, hints about this (3 really small C filefollows)?
Basically I used __attribute__((constructor)) gcc extension.
I'm aware that this is not a really portable feature (it's not
standard) but I think that many people are using gcc right now,
am I right? :)
--
Lorenzo Cavallaro `Gigi Sullivan' <sullivan@sikurezza.org>
Until I loved, life had no beauty;
I did not know I lived until I had loved. (Theodor Korner)
#include <stdio.h>
#ifndef __TMP1_H__
#define __TMP1_H__
#define DECLARE(x) char *x
#define INIT(x) (x) = foobar(#x)
void init_foobar(void) __attribute__((constructor));
char *foobar(char *);
DECLARE(foo);
DECLARE(bar);
#endif
#include <stdio.h>
#include "tmp1.h"
void
init_foobar(void)
{
printf("init_foobar gets executed\n");
INIT(foo);
INIT(bar);
return;
}
char *
foobar(char *s)
{
return s;
}
#include <stdio.h>
#include "tmp1.h"
int
main(void)
{
printf("main gets executed\n");
printf("foo: `%s'\n", foo);
printf("bar: `%s'\n", bar);
exit(0);
}