www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - extern, how does it work ( linker says undefined )

reply Carlos <carlos2003nov yahoo.ca> writes:
Here is a small main program + a subrouitne:

if i compile with:

   dmd main bsderr

the linker complains:
   bsderr.obj(bsderr)
   Error 42: Symbol Undefined _D6bsderr7programPa

i tried may scenarios of compiling and linking...
nothing works.

Can someone explains how to make this working.

Thanks

/* --- main.d : main file --- */
char* program;
import bsderr;
int main( char[][] argv )
{
   program = argv[0];
   warnx( "it works %s + %03d\n", cast(char*)"asdasdasd", 23 );
   return 0;
}

/* --- bsderr.d : a subroutine --- */
import std.c.stdio;
import std.c.stddef;
import std.c.stdarg;

extern char* program;  /* defined in the main program */

void warnx( char* format, ... )
{
   va_list ap;
   va_start!(char*)(ap,format);
   if( program && *program )
     fprintf( stderr, "%s: ", program );
   vfprintf( stderr, format, ap );
   va_end (ap);
}
Mar 13 2005
next sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Carlos <carlos2003nov yahoo.ca> wrote:

[...]
 the linker complains:
    bsderr.obj(bsderr)
    Error 42: Symbol Undefined _D6bsderr7programPa

Change extern char* program; to extern (D) char* program; ( http://www.digitalmars.com/d/attribute.html ) Issue commands dmd -c bsderr dmd main bsderr.obj -manfred
Mar 14 2005
next sibling parent reply "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak  
<svv1999 hotmail.com> wrote:
 Carlos <carlos2003nov yahoo.ca> wrote:

 [...]
 the linker complains:
    bsderr.obj(bsderr)
    Error 42: Symbol Undefined _D6bsderr7programPa

Change extern char* program; to extern (D) char* program; ( http://www.digitalmars.com/d/attribute.html )

Does that mean "extern" is the same as "extern (C)"? Because, from the above link: <quote> C function calling conventions are specified by: extern (C): int foo();call foo() with C conventions D conventions are: extern (D): or: extern: </quote> it seems to suggest "extern (D)" should be the same as "extern". Regan
Mar 14 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
"Regan Heath" <regan netwin.co.nz> wrote:

[...]
 it seems to suggest "extern (D)" should be the same as "extern".

I agree. But the specs also say: | C and D must be supplied So the specs are ambiguous. However, a discussion on the specs wouldn't help Carlos. -manfred
Mar 14 2005
next sibling parent reply Carlos <carlos2003nov yahoo.ca> writes:
Manfred Nowak wrote:
 "Regan Heath" <regan netwin.co.nz> wrote:
 
 [...]
 
it seems to suggest "extern (D)" should be the same as "extern".

I agree. But the specs also say: | C and D must be supplied So the specs are ambiguous. However, a discussion on the specs wouldn't help Carlos. -manfred

I think that part of the specs, says: "All implementations of D, must support at least two linkage, ie: C and D. Others can be supported, but it's not mandatory". This could be clarified.
Mar 14 2005
parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Carlos <carlos2003nov yahoo.ca> wrote:

[...]
 "All implementations of D, must support at least two
 linkage, ie: C and D. Others can be supported, but it's not
 mandatory".

Agreed. Walter seems to have switched from a programmers point of view on D to a compiler writers point of view on D. I this even clearer: "Main linkage types are C and D. In addition under windows the linkage types Windows and Pascal exist. Further linkage types may exist." -manfred
Mar 14 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Manfred Nowak wrote:

 Agreed. Walter seems to have switched from a programmers point of 
 view on D to a compiler writers point of view on D.

I'm not sure that Walter ever switched *from* the compiler writers point of view on D... :-) --anders
Mar 14 2005
prev sibling parent "Regan Heath" <regan netwin.co.nz> writes:
On Mon, 14 Mar 2005 10:36:49 +0000 (UTC), Manfred Nowak  
<svv1999 hotmail.com> wrote:
 "Regan Heath" <regan netwin.co.nz> wrote:
 So the specs are ambiguous. However, a discussion on the specs
 wouldn't help Carlos.

True, but you've already done that. :) Regan
Mar 14 2005
prev sibling parent reply Derek Parnell <derek psych.ward> writes:
On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak wrote:

 Carlos <carlos2003nov yahoo.ca> wrote:
 
 [...]
 the linker complains:
    bsderr.obj(bsderr)
    Error 42: Symbol Undefined _D6bsderr7programPa

Change extern char* program; to extern (D) char* program;

Curiously if you use "extern (C) char* program;" it also compiles and runs correctly. This might be a bug, in either dmd or the documentation. -- Derek Parnell Melbourne, Australia 14/03/2005 9:45:57 PM
Mar 14 2005
next sibling parent Carlos <carlos2003nov yahoo.ca> writes:
Derek Parnell wrote:
 On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak wrote:

 
 Curiously if you use "extern (C) char* program;" it also compiles and runs
 correctly. This might be a bug, in either dmd or the documentation.
 

--[ Based on dmd 0.118 ]-- Yes i discovered that also. More, if you use extern (C) char*program; the whole thing can be compiled with one command: ie: dmd main bsderr but if you use extern (C) char*program; you must issue 2 separate commands ie: dmd -c main dmd main bsderr Also, This is two D files. I think "extern" is synonym of "extern (D)" because "D" is the default linkage if no one is specified. So, i think this is a bug !
Mar 14 2005
prev sibling parent John Reimer <brk_6502 yahoo.com> writes:
Derek Parnell wrote:
 On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak wrote:
 
 
Carlos <carlos2003nov yahoo.ca> wrote:

[...]

the linker complains:
   bsderr.obj(bsderr)
   Error 42: Symbol Undefined _D6bsderr7programPa

[...] Change extern char* program; to extern (D) char* program;

Curiously if you use "extern (C) char* program;" it also compiles and runs correctly. This might be a bug, in either dmd or the documentation.

But don't extern(D) and extern(C) have different name mangling affects also? extern(D) usually appends the current module/namespace to the name, while extern(C) typically keeps the symbol name undecorated. -JJR
Mar 14 2005
prev sibling parent reply jicman <jicman_member pathlink.com> writes:
Carlos,

you may want to take a look at build.exe which will take away the need to
provide

dmd source.d source0.d source1.d ... sourceN.d

to the simple and more beautiful command,

build source.0

and build.exe will do all the linking for you.  I am loving this little but
powerful application more and more each day. 

Oh yeah, back to your problem: you need to specify that bsderr.d is piece of
software that could be link.  In top of you your bsderr.d file you need to
specify this.  With this simple command,


/* --- bsderr.d : a subroutine --- */
modudle bsderr;
import std.c.stdio;
import std.c.stddef;
import std.c.stdarg;
..
..

that should do it.  One last piece of advice: you may want to privately import
the libraries in your libraries.  This simple means that instead of

import std.c.stdio;
import std.c.stddef;
import std.c.stdarg;

you now have

private import std.c.stdio;
private import std.c.stddef;
private import std.c.stdarg;

Just a thought...

thanks,

josť




Carlos says...
Here is a small main program + a subrouitne:

if i compile with:

   dmd main bsderr

the linker complains:
   bsderr.obj(bsderr)
   Error 42: Symbol Undefined _D6bsderr7programPa

i tried may scenarios of compiling and linking...
nothing works.

Can someone explains how to make this working.

Thanks

/* --- main.d : main file --- */
char* program;
import bsderr;
int main( char[][] argv )
{
   program = argv[0];
   warnx( "it works %s + %03d\n", cast(char*)"asdasdasd", 23 );
   return 0;
}

/* --- bsderr.d : a subroutine --- */
import std.c.stdio;
import std.c.stddef;
import std.c.stdarg;

extern char* program;  /* defined in the main program */

void warnx( char* format, ... )
{
   va_list ap;
   va_start!(char*)(ap,format);
   if( program && *program )
     fprintf( stderr, "%s: ", program );
   vfprintf( stderr, format, ap );
   va_end (ap);
}

Mar 14 2005
parent Carlos <carlos2003nov yahoo.ca> writes:
jicman wrote:
 Carlos,
 
 you may want to take a look at build.exe which will take away the need to
 provide
 

Yes, i known build. I use it, and it's quite good. That's utility has a future, for sure...
Mar 14 2005