Mail Archives: djgpp/1997/07/10/22:19:38
Andrew Crabtree wrote:
>
>
> Could someone explain how the .ctor section works for DJGPP
> and if it actually works OK?
I know how it works and that it works but (sorry) I cannot explain
it here in short words.
> it can't get it to (or tell if it is) work properly.
>
> If I compile the following code
Your example and the ctor topic have nothing to do with each
other.
> As C the compiler complains that it is using a non-constant initializer.
Exact, and when I remember correct (I have not POSIX or ANSI documents)
this is not allowed at all!
> First I'm not sure if this is right. I believe (2nd hand reference),
> that this works with linux gcc. I wouldn't normally code something that
You are wrong. Here some output from a linux machine:
rho AT pandora-508 $ cat test.c
#include <stdio.h>
int f()
{
return 5;
}
int x = f();
main()
{
printf("x = %d\n",x);
return 0;
}
rho AT pandora-509 $ gcc -v -c test.c
Reading specs from /usr/lib/gcc-lib/i386-linux/2.7.2.1/specs
gcc version 2.7.2.1
/usr/lib/gcc-lib/i386-linux/2.7.2.1/cpp -lang-c -v -undef -D__GNUC__=2
-D__GNUC_MINOR__=7 -D__ELF__ -Dunix -Di386 -Dlinux -D__ELF__ -D__unix__
-D__i386__ -D__linux__ -D__unix -D__i386 -D__linux -Asystem(unix)
-Asystem(posix) -Acpu(i386) -Amachine(i386) test.c /tmp/cca31189.i
GNU CPP version 2.7.2.1 (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/include
/usr/i386-linux/include
/usr/lib/gcc-lib/i386-linux/2.7.2.1/include
/usr/include
End of search list.
/usr/lib/gcc-lib/i386-linux/2.7.2.1/cc1 /tmp/cca31189.i -quiet
-dumpbase test.c -version -o /tmp/cca31189.s
GNU C version 2.7.2.1 (i386 Linux/ELF) compiled by GNU C version
2.7.2.1.
test.c:7: initializer element is not constant
rho AT pandora-510 $
The solution for you problem should be the following:
#include <stdio.h>
int f()
{
return 5;
}
int x;
static void _init_my_variables() __attribute__((__constructor__));
static void _init_my_variables()
{
x = f();
}
main()
{
printf("x = %d\n",x);
return 0;
}
--
*****************************************************************
* Robert Hoehne, Fakultaet fuer Mathematik, TU-Chemnitz-Zwickau *
* Post: Am Berg 3, D-09573 Dittmannsdorf *
* e-Mail: Robert DOT Hoehne AT Mathematik DOT TU-Chemnitz DOT DE *
* WWW: http://www.tu-chemnitz.de/~rho *
*****************************************************************
- Raw text -