Re: extern "C"

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Tim Roberts wrote:
George <George@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

Just to confirm what is the most correct way beyond a just working
function.

1.

We need to add extern "C" to both variable/function
definition/declaration? Or only need to add to the variable/function
declaration?

2.

How about extern? Declaration only or both declaration and
definition are required?

BTW: previously, I only add to declaration, but after reading more
and more code which add to both declaration and definition, I come
to here to ask this question.

extern "C" is only required for the declaration.

Do you understand that extern "C" is only used for C++ that you need
to call from a C function?

extern "C" should also be used with dllexport, even if the caller is C++, to
get rid of the (compiler version dependent) name mangling.


.



Relevant Pages

  • 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)
  • Is the following undefined behavior?
    ... static int i; ... extern int i; ... declaration is visible with linkage, ...
    (comp.std.c)