View previous topic :: View next topic |
Author |
Message |
Jedive
Joined: 01 Jan 2007 Posts: 7
|
Posted: Sat Sep 22, 2007 3:11 pm Post subject: static methods? |
|
|
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 |
|
|
Derek Parnell
Joined: 22 Apr 2004 Posts: 408 Location: Melbourne, Australia
|
Posted: Sun Sep 23, 2007 5:55 pm Post subject: Re: static methods? |
|
|
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 |
|
|
Jedive
Joined: 01 Jan 2007 Posts: 7
|
Posted: Mon Sep 24, 2007 8:23 am Post subject: |
|
|
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 |
|
|
csauls
Joined: 27 Mar 2004 Posts: 278
|
Posted: Mon Sep 24, 2007 3:23 pm Post subject: |
|
|
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 |
|
|
|