extern intended behavior?

Sean Kelly sean at f4.ca
Tue Jun 13 09:13:00 PDT 2006


David Medlock wrote:
> Look at the following source files(main.d and test.d):
> 
> // main.d  ----------------------
> extern
> {
>   void MyFunction();
> }
> 
> void main(char[][] args )
> {
>   MyFunction();
> }
> 
> 
> // test.d ----------------------
> 
> import std.stdio;
> 
> void MyFunction()
> {
>   writefln("Hello World");
> }
> 
> compiles fine, but linker complains:
>  Error 42: Symbol Undefined _D4main10MyFunctionFZv
> --- errorlevel 1
> 
> 
> It appears the linker expects the extern function to be in the 'main' 
> module.  This appears to be incorrect behavior.  If it isn't why such a 
> departure from a common C idiom(lex and yacc).
> 
> Am I missing something?

In D the symbol name is based on the module name, which is equivalent to 
  the file name in the absence of a module statement.  For this reason, 
"extern (D)" has very limited use.  Generally, I'll only use it if I'm 
doing something like this:

     extern (C):
     ...
     extern (D) void inlineFunc() {}

Sean



More information about the Digitalmars-d mailing list