www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - linking in linux

reply llee <llee goucher.edu> writes:
I'm having problems calling functions that are defined in other .d files under
linux.
Whenever I make a function call to a function that is defined in another .d
file, the linker (ld) returns an error (exit code 1) saying that the referenced
function is undefined.
I have a program that is trying to call a function named distorm_decode (...).
This function is declared in the file distorm.d. The function is defined in a
file named distorm.c. This file was compiled using the gcc compiler and stored
in a .a archive (library) using ar. When I try to compile my program ld returns
the following error:

d_disassembler.d:(.gnu.linkonce.t_Dmain + 0x10c): undefined reference to
'distorm_decode'
collect2: ld returned 1 exit status
--errorlevel 1

This is not the first time that I've encountered this problem. Other programs
have also failed to compile, even when the function is defined in another d
module. 

Any help would be appreciated.
Sep 27 2007
parent reply BCS <ao pathlink.com> writes:
Reply to llee,

 I'm having problems calling functions that are defined in other .d
 files under linux.
 
 Whenever I make a function call to a function that is defined in
 another .d file, the linker (ld) returns an error (exit code 1) saying
 that the referenced function is undefined.
 
 I have a program that is trying to call a function named
 distorm_decode (...). This function is declared in the file distorm.d.
 The function is defined in a file named distorm.c. This file was
 compiled using the gcc compiler and stored in a .a archive (library)
 using ar. When I try to compile my program ld returns the following
 error:
 
 d_disassembler.d:(.gnu.linkonce.t_Dmain + 0x10c): undefined reference
 to 'distorm_decode'
 collect2: ld returned 1 exit status
 --errorlevel 1
 This is not the first time that I've encountered this problem. Other
 programs have also failed to compile, even when the function is
 defined in another d module.
 
 Any help would be appreciated.
 
A little source code would help (the d file for starters). My only guess from what you have sad would be that the declaration is wrong. Two things come to mind: Are you sure that the function is being compiled as a C function and not a C++ function? Did you use extern(C) in the d file? to check that one.
Sep 27 2007
next sibling parent reply Regan Heath <regan netmail.co.nz> writes:
BCS wrote:
 Reply to llee,
 
 I'm having problems calling functions that are defined in other .d
 files under linux.

 Whenever I make a function call to a function that is defined in
 another .d file, the linker (ld) returns an error (exit code 1) saying
 that the referenced function is undefined.

 I have a program that is trying to call a function named
 distorm_decode (...). This function is declared in the file distorm.d.
 The function is defined in a file named distorm.c. This file was
 compiled using the gcc compiler and stored in a .a archive (library)
 using ar. When I try to compile my program ld returns the following
 error:

 d_disassembler.d:(.gnu.linkonce.t_Dmain + 0x10c): undefined reference
 to 'distorm_decode'
 collect2: ld returned 1 exit status
 --errorlevel 1
 This is not the first time that I've encountered this problem. Other
 programs have also failed to compile, even when the function is
 defined in another d module.

 Any help would be appreciated.
A little source code would help (the d file for starters).
And the command line used to compile and link. Regan
Sep 28 2007
parent reply llee <llee goucher.edu> writes:
I wrote a test program to isolate the problem. The following program
(program.d) calls a function named test_function() that is defined in test.d.
test.d is displayed after it. When I try to compile the the program I receive
an error.

program.d:
------
import test;
void main ()
{
test_function ();
}

test.d:
------

import std.cstream;
void test_function ()
{
dout.writeLine ("test function called.");
{

Compilation:
------
dmd program.d 
...
program.o: (.data+0x30): undefined reference to '_D11test_module12_ModuleInfoZ'
program.o (.gnu.linkonce.t_Dmain+0x4): undefined reference to
'_D11test_module13test_functionFZv'
collect2: ld returned 1 exit status
--errorlevel 1
Sep 28 2007
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"llee" <llee goucher.edu> wrote in message 
news:fdj3h4$m3j$1 digitalmars.com...
I wrote a test program to isolate the problem. The following program 
(program.d) calls a function named test_function() that is defined in 
test.d. test.d is displayed after it. When I try to compile the the program 
I receive an error.

 program.d:
 ------
 import test;
 void main ()
 {
 test_function ();
 }

 test.d:
 ------

 import std.cstream;
 void test_function ()
 {
 dout.writeLine ("test function called.");
 {

 Compilation:
 ------
 dmd program.d
try dmd program.d test.d The linker can't find test.o because you didn't tell it to compile it. -Steve
Sep 28 2007
prev sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:ce0a334323bfe8c9cf6333c514f2 news.digitalmars.com...

nm libxyz.a This will list all the symbols defined in libxyz.a. You can do: nm libxyz.a | grep distorm To see if the name is mangled. You can also do: nm -C libxyz.a To have nm demangle the symbols for you You can also do: nm -D libxyz.so to list symbols in a dynamic library. -Steve
Sep 28 2007
parent BCS <ao pathlink.com> writes:
Reply to Steven,

 "BCS" <ao pathlink.com> wrote in message
 news:ce0a334323bfe8c9cf6333c514f2 news.digitalmars.com...
 

 
nm libxyz.a This will list all the symbols defined in libxyz.a. You can do: nm libxyz.a | grep distorm To see if the name is mangled. You can also do: nm -C libxyz.a To have nm demangle the symbols for you You can also do: nm -D libxyz.so to list symbols in a dynamic library. -Steve
cool, I'll have to remember that
Sep 28 2007