Thread overview
Import error
Nov 11, 2010
Carlo
Nov 11, 2010
Simen kjaeraas
Nov 11, 2010
bearophile
November 11, 2010
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?

November 11, 2010
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
November 11, 2010
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
November 11, 2010
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