Pascal Viandier wrote:
-----Message d'origine----- De : gpc-owner@gnu.de [mailto:gpc-owner@gnu.de] De la part de Frank Heckenbach Envoyé : March 15, 2006 11:29 à: gpc@gnu.de Objet : Re: export vs static
Pascal Viandier wrote:
I would like to submit a case on which I spent some time. It occurred with sources converted to GPC from SUN Pascal with an automated script.
In the interface section of a module, the following statements:
Export MyModule = (A); .... Var A : Integer = 4; attribute(static); ....
This module compiles with no complaint. However for all modules importing MyModule -AND- using A, gpc issues the <error undefined reference to 'A'>.
The problem is: SUN Pascal understands the "static" directive as in C, so
the
statements above are correct and work - as in C -, but not for GPC.
How so? In C, static means file-scope, i.e. static declarations are not visible from other files:
/* a.c */
static int a;
/* b.c */
extern int a;
int main () { a = 42; }
# gcc a.c b.c /tmp/ccKBFfHh.o: In function `main': b.c:(.text+0x1e): undefined reference to `a'
Doesn't work in C.
Sorry, my explanation was missing details. The interface section was previously in a header file - like in C - included with #include in other modules. All these modules get their own copy of the variable with the same initial value. This was mainly used for default values and it was the way to do it because SUN Pascal did not allow initial value to variables in the declaration part (at least the version used at this time).
A separate copy per module is quite different from ex-/importing a variable. If that's what you want, you have to declare the variable in each module in GPC (possibly using an include file). No amount of trickery with ex-/import will get you this.
Or don't you ever change the variable (so it doesn't matter whether or not they are identical)? In this case, `static int' (or better `static const int') might be suitable in C, but in Pascal, you'd better declare a constant (`const A = 4;') which can be ex-/imported, and there are no questions about static or not.
Also with SUN Pascal all symbols are exported by default (again like in C).
No. In C, nothing is exported by default. You have to put each declaration into a header file yourself.
Yes, it's a nonstandard extension.
A useful one indeed. But a bit misleading with its C homonym ;-)
As far as the matters above, GPC's `static' is just like C's.
You're really comparing different things. On Sun Pascal and C, you include a *separate* copy of the variable in each module, so each of them has to be restricted to the module's scope to avoid conflicts. That's what static does.
In GPC, you ex-/import the *same* variable in different modules. Therefore it must not be restricted per module so that it's accessible everywhere. That's what non-static does.
The same holds in C. The proper way in C to share a variable is to define it in one .c file:
int a = 42;
and declare it extern in a .h file included by all files that need it:
extern int a;
Such variables cannot be static in C either.
Don't know about Sun Pascal, but so far it seems similar to C.
Frank