www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Import error

reply Carlo <carlo.lucibello gmail.com> writes:
Hi everyone,
i'm having trouble with the statement import. Here is an example:

//file ~/d/libtest.d
int fun(int x){
     return x;
}
//endfile

//file ~/d/test.d
import std.stdio;
import libtest;

void main(){
     writeln(fun(2));
}
//endfile


I issue the commands
#cd ~/d
#dmd test.d

and i get the error

test.d:(.text._Dmain+0x9): undefined reference to `_D7libtest3funFiZi'
collect2: ld returned 1 exit status
--- errorlevel 1

I have no troubles with standard libraries, just mine's. I have the dmd 
2.0.50 compiler on a GNU/Linux 64bit system. Any idea?
Nov 11 2010
next sibling parent "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Carlo <carlo.lucibello gmail.com> wrote:

 test.d:(.text._Dmain+0x9): undefined reference to `_D7libtest3funFiZi'
 collect2: ld returned 1 exit status
 --- errorlevel 1

 I have no troubles with standard libraries, just mine's. I have the dmd  
 2.0.50 compiler on a GNU/Linux 64bit system. Any idea?

You must pass all source files on the command line: dmd test.d libtest.d -- Simen
Nov 11 2010
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 11 Nov 2010 07:44:22 -0500, Carlo <carlo.lucibello gmail.com>  
wrote:

 Hi everyone,
 i'm having trouble with the statement import. Here is an example:

 //file ~/d/libtest.d
 int fun(int x){
      return x;
 }
 //endfile

 //file ~/d/test.d
 import std.stdio;
 import libtest;

 void main(){
      writeln(fun(2));
 }
 //endfile


 I issue the commands
 #cd ~/d
 #dmd test.d

 and i get the error

 test.d:(.text._Dmain+0x9): undefined reference to `_D7libtest3funFiZi'
 collect2: ld returned 1 exit status
 --- errorlevel 1

 I have no troubles with standard libraries, just mine's. I have the dmd  
 2.0.50 compiler on a GNU/Linux 64bit system. Any idea?

dmd test.d libtest.d "undefined reference" usually means it's a link error, meaning it can't find the code that implements that function. -Steve
Nov 11 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Steven Schveighoffer:

 "undefined reference" usually means it's a link error, meaning it can't  
 find the code that implements that function.

I have seen plenty of people fall in this trap/error (me too, at the beginning), people are often used to Python or Java, that don't have this problem. So I'd like a better error message here (but generally improving linker error messages isn't easy). Bye, bearophile
Nov 11 2010