View previous topic :: View next topic |
Author |
Message |
alienhunter3
Joined: 13 Apr 2007 Posts: 3
|
Posted: Fri Apr 13, 2007 5:22 pm Post subject: one of those noob questions..... |
|
|
Hey,
I'm new to D, and relatively new to serious programming. I know python pretty well, and I know a fair amount of java from school. (I've also dabbled in C++). I was looking to learn a compiled language, but wanted an easier OOP system than C++, and so I found D, which looks really really cool. I have been looking to the solution to the problem that I'm having but can't find it, probably because it is such a noobish problem that nobody bothers
Oh, and I have dmd installed correctly (I use gentoo linux), and I can compile the basic helloworld program.
Now I'm rather embarrassed for asking this question, but how do you import classes that are in other .d files that you've written. Here are some code examples.
(Don't laugh at my code examples, I'm just trying to get the concept down )
rectangle.d
Code: | import std.stdio;
import std.string;
public class Rectangle{
private int length;
private int width;
this(int theLength, int theWidth){
length = theLength;
width = theWidth;
}
public int getLength(){
return length;
}
public int getWidth(){
return width;
}
public char[] toString(){
char[] toReturn = "(" ~ std.string.toString(length) ~ "," ~ std.string.toString(width) ~ ")";
return toReturn;
}
}
|
That's a class that I have written. I have no errors when I run
Now I have another, extremely simple d file (test2.d) that uses rectangle.d:
Code: |
import std.stdio;
import rectangle;
void main(){
Rectangle x = new Rectangle(3,4);
writefln(x.toString());
}
|
both of these files are in the same directory, but here's what i get when i try to compile it:
Code: | alienhunter@beta5 ~/documents/school/programming/dLang $ ls
libs rectangle.d rectangle.o test2.d test2.o
alienhunter@beta5 ~/documents/school/programming/dLang $ dmd test2.d -v
parse test2
semantic test2
import object (/usr/local/include/phobos/object.d)
import std.stdio (/usr/local/include/phobos/std/stdio.d)
import std.c.stdio (/usr/local/include/phobos/std/c/stdio.d)
import std.c.stddef (/usr/local/include/phobos/std/c/stddef.d)
import std.c.stdarg (/usr/local/include/phobos/std/c/stdarg.d)
import std.format (/usr/local/include/phobos/std/format.d)
import std.stdarg (/usr/local/include/phobos/std/stdarg.d)
import std.utf (/usr/local/include/phobos/std/utf.d)
import std.c.stdlib (/usr/local/include/phobos/std/c/stdlib.d)
import std.c.string (/usr/local/include/phobos/std/c/string.d)
import std.string (/usr/local/include/phobos/std/string.d)
import std.uni (/usr/local/include/phobos/std/uni.d)
import std.array (/usr/local/include/phobos/std/array.d)
import std.ctype (/usr/local/include/phobos/std/ctype.d)
import std.gc (/usr/local/include/phobos/std/gc.d)
import gcstats (/usr/local/include/phobos/gcstats.d)
import std.c.linux.linux (/usr/local/include/phobos/std/c/linux/linux.d)
import std.c.linux.linuxextern (/usr/local/include/phobos/std/c/linux/linuxextern.d)
import std.c.linux.pthread (/usr/local/include/phobos/std/c/linux/pthread.d)
import std.intrinsic (/usr/local/include/phobos/std/intrinsic.d)
import rectangle (rectangle.d)
semantic2 test2
semantic3 test2
code test2
function main
gcc test2.o -o test2 -m32 -lphobos -lpthread -lm
test2.o:(.data+0x38): undefined reference to `_D9rectangle12__ModuleInfoZ'
test2.o: In function `_Dmain':
test2.d:(.gnu.linkonce.t_Dmain+0x4): undefined reference to `_D9rectangle9Rectangle7__ClassZ'
test2.d:(.gnu.linkonce.t_Dmain+0x13): undefined reference to `_D9rectangle9Rectangle5_ctorMFiiZ C9rectangle9Rectangle'
collect2: ld returned 1 exit status
--- errorlevel 1
|
I am sure that i've missed something really really simple, so I apologize in advance for that, but any help would be appreciated.
Thanks, and I look forward to working with you guys, in the future. |
|
Back to top |
|
|
alienhunter3
Joined: 13 Apr 2007 Posts: 3
|
Posted: Fri Apr 13, 2007 10:15 pm Post subject: partial update |
|
|
I think that the problem is occurring when gcc does the linking, rather than when D does the importing. Some problem with the gcc path, perhaps? |
|
Back to top |
|
|
Bradley Smith
Joined: 20 Jun 2006 Posts: 60
|
Posted: Fri Apr 13, 2007 10:47 pm Post subject: |
|
|
You need to include the object or source file for the modules being imported.
Try Code: | dmd test2.d rectangle.o | or Code: | dmd test2.d rectangle.d |
|
|
Back to top |
|
|
alienhunter3
Joined: 13 Apr 2007 Posts: 3
|
Posted: Fri Apr 13, 2007 10:54 pm Post subject: |
|
|
that did it. thanks! |
|
Back to top |
|
|
davidb
Joined: 03 Apr 2007 Posts: 15
|
Posted: Sun Apr 22, 2007 12:06 pm Post subject: |
|
|
I'd suggest using a tool like build(bud)/rebuild/dsss
which automates the task, you'll appreciate it
the longer your projects become.
david |
|
Back to top |
|
|
zzzzrrr
Joined: 17 Feb 2007 Posts: 139 Location: Washington, DC
|
Posted: Mon Apr 23, 2007 12:19 pm Post subject: |
|
|
david wrote: | I'd suggest using a tool like build(bud)/rebuild/dsss
which automates the task, you'll appreciate it
the longer your projects become.
david |
I second that. DSSS does all the linking for you and makes it very easy. I don't even use a makefile for my programs.....
-Mason |
|
Back to top |
|
|
|