FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

one of those noob questions.....

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
alienhunter3



Joined: 13 Apr 2007
Posts: 3

PostPosted: Fri Apr 13, 2007 5:22 pm    Post subject: one of those noob questions..... Reply with quote

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 Sad

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 Very Happy )

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

Code:

dmd rectangle.d -c


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
View user's profile Send private message Yahoo Messenger
alienhunter3



Joined: 13 Apr 2007
Posts: 3

PostPosted: Fri Apr 13, 2007 10:15 pm    Post subject: partial update Reply with quote

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
View user's profile Send private message Yahoo Messenger
Bradley Smith



Joined: 20 Jun 2006
Posts: 60

PostPosted: Fri Apr 13, 2007 10:47 pm    Post subject: Reply with quote

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
View user's profile Send private message
alienhunter3



Joined: 13 Apr 2007
Posts: 3

PostPosted: Fri Apr 13, 2007 10:54 pm    Post subject: Reply with quote

that did it. thanks!
Back to top
View user's profile Send private message Yahoo Messenger
davidb



Joined: 03 Apr 2007
Posts: 15

PostPosted: Sun Apr 22, 2007 12:06 pm    Post subject: Reply with quote

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
View user's profile Send private message
zzzzrrr



Joined: 17 Feb 2007
Posts: 139
Location: Washington, DC

PostPosted: Mon Apr 23, 2007 12:19 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group