www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - -run with import gives OPTLINK Error 42

reply "MikeWh" <cf725b-0 yahoo.com> writes:
I have 2 files: 1. a main file called caller.d, 2. a file to import called 
sub.d.  They are:

// caller.d
import sub;
import std.stdio;
void main(){
    sub mySub= new sub;
    writefln( mySub.one() );
    }

// sub.d
class sub{
    int one(){
        return 1;
        }
     }

Everything compiles fine using "dmd caller.d sub.d", and correctly writes 
"1" when I execute "caller".
But if I try "dmd -run caller.d sub.d" I get "OPTLINK Error 42: Symbol 
Undefined _D3sub3sub7_ClassZ"
I get the error even if I first do "dmd -lib sub.d".  Same thing happens in 
D 1.043 as D 2.028.

Anyone know what I'm doing wrong here? 
May 16 2009
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
MikeWh wrote:
 I have 2 files: 1. a main file called caller.d, 2. a file to import called 
 sub.d.  They are:
 
 // caller.d
 import sub;
 import std.stdio;
 void main(){
     sub mySub= new sub;
     writefln( mySub.one() );
     }
 
 // sub.d
 class sub{
     int one(){
         return 1;
         }
      }
 
 Everything compiles fine using "dmd caller.d sub.d", and correctly writes 
 "1" when I execute "caller".
 But if I try "dmd -run caller.d sub.d" I get "OPTLINK Error 42: Symbol 
 Undefined _D3sub3sub7_ClassZ"
 I get the error even if I first do "dmd -lib sub.d".  Same thing happens in 
 D 1.043 as D 2.028.
 
 Anyone know what I'm doing wrong here? 
You can only specify one source file after -run. The rest of the command line is used as the command line for the program being run. Since sub.d isn't compiled and linked in, you get the undefined symbol before it gets that far. What you want to use is "dmd sub.d -run caller.d".
May 16 2009
parent "MikeWh" <cf725b-0 yahoo.com> writes:
Argh!  So simple, but it's been driving me crazy for the past two days.
Thank you.

"Frits van Bommel" <fvbommel REMwOVExCAPSs.nl> wrote in message 
news:guni54$2r4s$1 digitalmars.com...
 MikeWh wrote:
 I have 2 files: 1. a main file called caller.d, 2. a file to import 
 called sub.d.  They are:

 // caller.d
 import sub;
 import std.stdio;
 void main(){
     sub mySub= new sub;
     writefln( mySub.one() );
     }

 // sub.d
 class sub{
     int one(){
         return 1;
         }
      }

 Everything compiles fine using "dmd caller.d sub.d", and correctly writes 
 "1" when I execute "caller".
 But if I try "dmd -run caller.d sub.d" I get "OPTLINK Error 42: Symbol 
 Undefined _D3sub3sub7_ClassZ"
 I get the error even if I first do "dmd -lib sub.d".  Same thing happens 
 in D 1.043 as D 2.028.

 Anyone know what I'm doing wrong here?
You can only specify one source file after -run. The rest of the command line is used as the command line for the program being run. Since sub.d isn't compiled and linked in, you get the undefined symbol before it gets that far. What you want to use is "dmd sub.d -run caller.d".
May 16 2009