digitalmars.D - extern, how does it work ( linker says undefined )
- Carlos (33/33) Mar 13 2005 Here is a small main program + a subrouitne:
- Manfred Nowak (12/15) Mar 14 2005 [...]
- Regan Heath (15/26) Mar 14 2005 Does that mean "extern" is the same as "extern (C)"?
- Manfred Nowak (7/8) Mar 14 2005 I agree. But the specs also say:
- Carlos (6/21) Mar 14 2005 I think that part of the specs, says:
- Manfred Nowak (11/14) Mar 14 2005 [...]
- =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= (4/6) Mar 14 2005 I'm not sure that Walter ever switched *from*
- Regan Heath (4/7) Mar 14 2005 True, but you've already done that. :)
- Derek Parnell (7/22) Mar 14 2005 Curiously if you use "extern (C) char* program;" it also compiles and ru...
- Carlos (16/21) Mar 14 2005 --[ Based on dmd 0.118 ]--
- John Reimer (5/30) Mar 14 2005 But don't extern(D) and extern(C) have different name mangling affects
- jicman (31/64) Mar 14 2005 Carlos,
- Carlos (4/9) Mar 14 2005 Yes, i known build.
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
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
On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak <svv1999 hotmail.com> wrote:Carlos <carlos2003nov yahoo.ca> wrote: [...]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". Reganthe 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 )
Mar 14 2005
"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
Manfred Nowak wrote:"Regan Heath" <regan netwin.co.nz> wrote: [...]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.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
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
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
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
On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak wrote:Carlos <carlos2003nov yahoo.ca> 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. -- Derek Parnell Melbourne, Australia 14/03/2005 9:45:57 PMthe linker complains: bsderr.obj(bsderr) Error 42: Symbol Undefined _D6bsderr7programPa[...] Change extern char* program; to extern (D) char* program;
Mar 14 2005
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
Derek Parnell wrote:On Mon, 14 Mar 2005 08:46:47 +0000 (UTC), Manfred Nowak wrote: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. -JJRCarlos <carlos2003nov yahoo.ca> 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.the linker complains: bsderr.obj(bsderr) Error 42: Symbol Undefined _D6bsderr7programPa[...] Change extern char* program; to extern (D) char* program;
Mar 14 2005
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
jicman wrote:Carlos, you may want to take a look at build.exe which will take away the need to provideYes, i known build. I use it, and it's quite good. That's utility has a future, for sure...
Mar 14 2005