Re: how to export global variable in C dll?

Tech-Archive recommends: Fix windows errors by optimizing your registry



"Cyrfer" wrote:
I think the fundamental lesson you were able to teach me is that
'declaring' is different from 'defining' ?

Yes, a declaration is not always a definition. The C++ standard
puts it succinctly:

<quote>
3.1 Declarations and definitions [basic.def]

1 A declaration introduces names into a translation unit or
redeclares names introduced by previous declarations. A
declaration specifies the interpretation and attributes of these
names.

2 A declaration is a definition unless it declares a function
without specifying the function's body, it contains the `extern'
specifier or a linkage-specification and neither an initializer
nor a function-body, it declares a static data member in a class
declaration, it is a class name declaration, or it is a typedef
declaration, a using-declaration, or a using-directive.
</quote>

Then there goes the list of examples of definitions and
declarations.

I mean, to export global variables in C DLLs, you need a header
file that declares the variable names in which the DLL and
client code will both reference (declared with extern). But
also, you need an implementation file that 'defines' the
variables (not using extern) - I've not been introduced to this
concept before.

You are correct. You need the `extern' specifier before the name
of a global variable to introdice a level of indirection. So, when
the name is used, the compiler will put a pointer to a definition,
which is resolved by linker later on.

Alex


.



Relevant Pages

  • Re: Can a static function declaration conflict with a non-static declaration?
    ... For an identifier declared with the storage-class specifier ... identifier is visible,23) if the prior declaration specifies ... internal or external linkage, ... "extern" keyword. ...
    (comp.lang.c)
  • Re: keyword extern
    ... >> definition or declaration until the end of the translation unit being ... >> One never needs to use the extern keyword with a function definition, ... >> ...then here is how a C compiler understands them. ... If any code in the source file access 'x', ...
    (comp.lang.c)
  • Re: "extern" inside a block
    ... In multi source file programs, ... > I know the behaviour when extern declaration follows a non-extern ... Linkage of Identifiers ... scope in which a prior declaration of that identifier is visible [and ...
    (comp.lang.c)
  • Re: extern
    ... > (of two files with int a; ... behaves as if it had been declared extern int a = 0; ... If the declaration of an identifier for ... function named by the identifier is a definition. ...
    (comp.lang.c)
  • Re: extern
    ... if you need a global varible declare it with external linkage, ... extern void f2; ... need not be defined in this translation unit. ... so as to confirm that the declaration matches the definition. ...
    (comp.lang.c)