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

static methods?

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



Joined: 01 Jan 2007
Posts: 7

PostPosted: Sat Sep 22, 2007 3:11 pm    Post subject: static methods? Reply with quote

Sorry if this has been discussed before. A quick search on the forums didn't reveal anything related.

The linker do not seem to find static methods from classes declared in external modules. I have made a really simple example to expose it.

Imagine we have a file "myclass.d". Its contents are the following:

Code:
import std.stdio;

class MyClass
{
    static void helloWorld()
    {
        writefln("Hello world!");
    }
}


Just a class with a static method which prints "Hello world!" on the screen.

The we have our main program "main.d":

Code:
import myclass;

void main()
{
    MyClass.helloWorld();
}


When I try to compile it, I get these errors:

C:\DOCUME~1\Jedive\CONFIG~1\Temp/cc4ecaaa.o(.text+0x7):main.d: undefined reference to `_D7myclass7MyClass10helloWorldFZv'
C:\DOCUME~1\Jedive\CONFIG~1\Temp/cc4ecaaa.o(.data+0x30):main.d: undefined reference to `_D7myclass12__ModuleInfoZ'
collect2: ld returned 1 exit status

I am using the GDC compiler, version 0.24, and GCC version 3.2.3
Back to top
View user's profile Send private message
Derek Parnell



Joined: 22 Apr 2004
Posts: 408
Location: Melbourne, Australia

PostPosted: Sun Sep 23, 2007 5:55 pm    Post subject: Re: static methods? Reply with quote

Jedive wrote:

The linker do not seem to find static methods from classes declared in external modules.

When I try to compile it, I get these errors:

Are you compiling it with both files on the command line?
Code:

gdc test.d myclass.d

With D, the "import" statement does not cause the imported file to be compiled into object code, it is just used by the compiler to assist in compiling the file that does the importing.
_________________
--
Derek
skype name: derek.j.parnell
Back to top
View user's profile Send private message
Jedive



Joined: 01 Jan 2007
Posts: 7

PostPosted: Mon Sep 24, 2007 8:23 am    Post subject: Reply with quote

That fixed it. Thank you very much. Since D modules remove the differences between a header file and its implementation, I didn't thought that you would have to tell the compiler twice that the file is part of your project.
Back to top
View user's profile Send private message
csauls



Joined: 27 Mar 2004
Posts: 278

PostPosted: Mon Sep 24, 2007 3:23 pm    Post subject: Reply with quote

The thing is, because there is no difference between header and implementation, you can very well have modules that are not linked in -- although this most commonly occurs as "interface modules" in .di files. It can also happen when a module only provides a view into another package (ie, it includes only public imports of other modules), or declarations of extern symbols supplied by a library (such as the Windows API symbols) which may be automatically linked. And so on.
_________________
Chris Nicholson-Sauls
Back to top
View user's profile Send private message AIM Address Yahoo Messenger
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