www.digitalmars.com         C & C++   DMDScript  

D - calling method from another 'd' file

reply paul stanton <paul_member pathlink.com> writes:
I'm a java background developer trying to get a feel for this language. come
across my first stumbling block. simply want to call a method from a different
'.d' file. i have seen this technique used in the wc.d/file.d example. cant
figure out why it wont work for me. my simple test example follows (keep in
mind, files are in same directory and i am trying to compile 'test1.d'). is
there something obvious that i have missed?

the result of my compile of test1.d....
test1.obj(test1)
Error 42: Symbol Undefined _Dtest2_testMethod_FZi
--- errorlevel 1

--------------
test1.d
--------------
import c.stdio;
import test2;

int main(char[][] args)
{
printf("%d", testMethod());
return 0;
}
--------------
test2.d
--------------
import c.stdio;

int testMethod()
{
return 1;
}
--------------
Jan 16 2003
parent reply Andy Friesen <andy ikagames.com> writes:
Try compiling both source files on the commandline in one call (ie "dmd 
test1.d test2.d").

The alternative is to build all your source files with the -c switch, 
which generates .OBJ (object code) for each source file, then link those 
all up with the link command.

  -- andy

paul stanton wrote:
 I'm a java background developer trying to get a feel for this language. come
 across my first stumbling block. simply want to call a method from a different
 '.d' file. i have seen this technique used in the wc.d/file.d example. cant
 figure out why it wont work for me. my simple test example follows (keep in
 mind, files are in same directory and i am trying to compile 'test1.d'). is
 there something obvious that i have missed?
 
 the result of my compile of test1.d....
 test1.obj(test1)
 Error 42: Symbol Undefined _Dtest2_testMethod_FZi
 --- errorlevel 1
 
 --------------
 test1.d
 --------------
 import c.stdio;
 import test2;
 
 int main(char[][] args)
 {
 printf("%d", testMethod());
 return 0;
 }
 --------------
 test2.d
 --------------
 import c.stdio;
 
 int testMethod()
 {
 return 1;
 }
 --------------
 
 

Jan 16 2003
parent reply Paul Stanton <Paul_member pathlink.com> writes:
Thanks. ok, now i think i understand the 3 files and their relevance...

exe - linked compiled code
obj - unlinked compiled code
map - linking information

if i am right (probably slightly off) is it true that the obj and map files are
irrelevant once the exe is created?

In article <b07mie$24ee$1 digitaldaemon.com>, Andy Friesen says...
Try compiling both source files on the commandline in one call (ie "dmd 
test1.d test2.d").

The alternative is to build all your source files with the -c switch, 
which generates .OBJ (object code) for each source file, then link those 
all up with the link command.

  -- andy

paul stanton wrote:
 I'm a java background developer trying to get a feel for this language. come
 across my first stumbling block. simply want to call a method from a different
 '.d' file. i have seen this technique used in the wc.d/file.d example. cant
 figure out why it wont work for me. my simple test example follows (keep in
 mind, files are in same directory and i am trying to compile 'test1.d'). is
 there something obvious that i have missed?
 
 the result of my compile of test1.d....
 test1.obj(test1)
 Error 42: Symbol Undefined _Dtest2_testMethod_FZi
 --- errorlevel 1
 
 --------------
 test1.d
 --------------
 import c.stdio;
 import test2;
 
 int main(char[][] args)
 {
 printf("%d", testMethod());
 return 0;
 }
 --------------
 test2.d
 --------------
 import c.stdio;
 
 int testMethod()
 {
 return 1;
 }
 --------------
 
 


Jan 16 2003
next sibling parent reply Paul Stanton <Paul_member pathlink.com> writes:
oh, another couple of questions....
how do i use the link command (tried and failed)
why when i compile the wc.d and wc2.d examples, dont i need to compile file.d in
the same call?

In article <b07mie$24ee$1 digitaldaemon.com>, Andy Friesen says...
Try compiling both source files on the commandline in one call (ie "dmd 
test1.d test2.d").

The alternative is to build all your source files with the -c switch, 
which generates .OBJ (object code) for each source file, then link those 
all up with the link command.

  -- andy

paul stanton wrote:
 I'm a java background developer trying to get a feel for this language. come
 across my first stumbling block. simply want to call a method from a different
 '.d' file. i have seen this technique used in the wc.d/file.d example. cant
 figure out why it wont work for me. my simple test example follows (keep in
 mind, files are in same directory and i am trying to compile 'test1.d'). is
 there something obvious that i have missed?
 
 the result of my compile of test1.d....
 test1.obj(test1)
 Error 42: Symbol Undefined _Dtest2_testMethod_FZi
 --- errorlevel 1
 
 --------------
 test1.d
 --------------
 import c.stdio;
 import test2;
 
 int main(char[][] args)
 {
 printf("%d", testMethod());
 return 0;
 }
 --------------
 test2.d
 --------------
 import c.stdio;
 
 int testMethod()
 {
 return 1;
 }
 --------------
 
 



Jan 16 2003
next sibling parent "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
----- Original Message -----
From: "Paul Stanton" <Paul_member pathlink.com>
Newsgroups: D
Sent: Thursday, January 16, 2003 9:16 PM
Subject: Re: calling method from another 'd' file


 oh, another couple of questions....
 how do i use the link command (tried and failed)

try using the -L switch as such: dmd -L test1.obj test2.obj
 why when i compile the wc.d and wc2.d examples, dont i need to compile

 the same call?

That's because file.d has been compiled and linked to the phosbos library. When the user needs it, the compiler already has a copy on hand in the library, so it gets it from there. Andrew
Jan 16 2003
prev sibling parent reply "Daniel Yokomiso" <daniel_yokomiso yahoo.com.br> writes:
"Paul Stanton" <Paul_member pathlink.com> escreveu na mensagem
news:b07p1k$25pg$1 digitaldaemon.com...
 oh, another couple of questions....
 how do i use the link command (tried and failed)
 why when i compile the wc.d and wc2.d examples, dont i need to compile

 the same call?

[snip] Hi, You can just use: dmd file1.d file2.d -c and then link file1.obj file2.obj It should be sufficient. I also struggled with compile and link steps (I'm also spoiled by Java's compiler). When your codebase gets larger it's better to get a build system. Dmd comes with a make like utility, but you can use ant if you're familiar with it. I think I'll write an article about 10 easy steps from java to D ;-) Best regards, Daniel Yokomiso. --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.443 / Virus Database: 248 - Release Date: 11/1/2003
Jan 17 2003
parent "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
 link file1.obj file2.obj

Thanks, I tried using this but it wouldn't work...had to inspect my settings and it turns out my environment variables were set incorrectly. Andrew
Jan 18 2003
prev sibling parent "Andrew Edwards" <aedwards spamfreeamerica.com> writes:
"Paul Stanton" <Paul_member pathlink.com> wrote in message
news:b07oq1$25j6$1 digitaldaemon.com...
 Thanks. ok, now i think i understand the 3 files and their relevance...

 exe - linked compiled code
 obj - unlinked compiled code
 map - linking information

 if i am right (probably slightly off) is it true that the obj and map

 irrelevant once the exe is created?

My understanding in my very short time at programming is that this is true for small, trivial programs. However, once your project size increases, those object files are used to create libraries. Which in turn, cuts down on the amount recompilations necessary for unchanged files.
Jan 16 2003