Suggestions for declarations.

A

artifact.one

Hi.

I'm working on a program that has one file of its source
code generated by a script that reads a colon delimited data
file and creates some C arrays as output. Now, in this data
file, some pieces of data refer to other pieces of data and
therefore, in these generated structures, there are pointers
to other generated data structures. The script is quite
brain dead. It doesn't try to order the data structures so
that the ones at the bottom of the chain of dependencies are
declared first (this might not even be possible, given the
data).

I thought a solution to this might be to (gasp!) put
declarations such as these at the top of the file:

extern const struct mkf_type *pretypes_execldep[];
extern const struct mkf_type *pretypes_cpjlib[];

And then the script will declare the actual structure later
in the same file:

const struct mkf_type *pretypes_cpjlib[] = {
&type_dynamicldep,
&type_dynamicldepx,
};

This way, I avoid any 'variable is undeclared at this point'
errors.

The trouble is, this gives the data structures external
linkage, which I really don't want. Making the array
declaration 'static' would, as far as I know, cause
undefined behaviour because of the previous 'extern'
declaration but I can't see that I can get rid of the
'extern' declaration given the ordering problem (some
structures refer to other structures that will not have been
declared yet if the 'extern' declaration is removed).

I realise this may sound like total gibberish, so be gentle
please. I've not completely grasped the intricacies
(horrors?) of the humble C compiler yet.

Any help would be appreciated.
MC
 
S

Spiros Bousbouras

Hi.

I'm working on a program that has one file of its source
code generated by a script that reads a colon delimited data
file and creates some C arrays as output. Now, in this data
file, some pieces of data refer to other pieces of data and
therefore, in these generated structures, there are pointers
to other generated data structures. The script is quite
brain dead. It doesn't try to order the data structures so
that the ones at the bottom of the chain of dependencies are
declared first (this might not even be possible, given the
data).

I thought a solution to this might be to (gasp!) put
declarations such as these at the top of the file:

extern const struct mkf_type *pretypes_execldep[];
extern const struct mkf_type *pretypes_cpjlib[];

And then the script will declare the actual structure later
in the same file:

const struct mkf_type *pretypes_cpjlib[] = {
&type_dynamicldep,
&type_dynamicldepx,
};

This way, I avoid any 'variable is undeclared at this point'
errors.

The trouble is, this gives the data structures external
linkage, which I really don't want. Making the array
declaration 'static' would, as far as I know, cause
undefined behaviour because of the previous 'extern'
declaration but I can't see that I can get rid of the
'extern' declaration given the ordering problem (some
structures refer to other structures that will not have been
declared yet if the 'extern' declaration is removed).

So you want the definition to be static but you're worried
that if you declared them extern earlier it will cause problems ?
Just declare them without using extern (or static).
 
A

artifact.one

Spiros said:
So you want the definition to be static but you're worried
that if you declared them extern earlier it will cause problems ?
Just declare them without using extern (or static).

Hello.

I tried this, but if I remove the early extern declaration and
make the second one static, the compiler rightfully complains:

type.c:28: error: static declaration of 'pretypes_cpjlib' follows
non-static declaration
type.c:21: error: previous declaration of 'pretypes_cpjlib' was here

If I remove the static keyword, using the nm tool, the symbols
appear to have external linkage again:

$ nm type.o
00000050 D pretypes_cpjlib

(uppercase 'D' is 'external linkage, in data segment')

MC
 
J

jacob navia

Hello.

I tried this, but if I remove the early extern declaration and
make the second one static, the compiler rightfully complains:

type.c:28: error: static declaration of 'pretypes_cpjlib' follows
non-static declaration
type.c:21: error: previous declaration of 'pretypes_cpjlib' was here

If I remove the static keyword, using the nm tool, the symbols
appear to have external linkage again:

$ nm type.o
00000050 D pretypes_cpjlib

(uppercase 'D' is 'external linkage, in data segment')

MC
You have to mark BOTH as static obviously
 
A

artifact.one

jacob said:
You have to mark BOTH as static obviously

Hi.

If I mark both as static, the compiler says:

type.c:36: error: array size missing in 'pretypes_cpjlib'

It no longer considers the first declaration as a sort of
"prototype" declaration (for want of a better term).

MC
 
J

jacob navia

Hi.

If I mark both as static, the compiler says:

type.c:36: error: array size missing in 'pretypes_cpjlib'

It no longer considers the first declaration as a sort of
"prototype" declaration (for want of a better term).

MC

Yes, I forgot to realize that.
Static arrays need a size specification and in your
declaration you do not put any size.

If you know the size you have to put it in there
(2) from your example

If you do NOT know the size you have to make two passes and
save the size.
 
A

artifact.one

jacob said:
Yes, I forgot to realize that.
Static arrays need a size specification and in your
declaration you do not put any size.

If you know the size you have to put it in there
(2) from your example

If you do NOT know the size you have to make two passes and
save the size.

Ah, simple as that eh?

Well, it does seem to work! Thanks!

MC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top