www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - using D with C++

reply Nical <nical.silva gmail.com> writes:
Hi, I'm trying to use C/C++ functions in a D program but I cant get
it to compile.
I tried the exemple of digital mars's website:

// ---- file.cpp
#include <iostream>
using namespace std;
int foo(int i, int j, int k)
{
    cout << "i = " << i << endl;
    cout << "j = " << j << endl;
    cout << "k = " << k << endl;
    return 7;
}

// ---- main.d
extern (C++) int foo(int i, int j, int k);
void main()
{
    foo(1,2,3);
}

and a makefile that might not be correct now that i've experimented
various things on it:

OBJ= main.o file.o

all: ${OBJ}
	g++ ${OBJ}

bin/main.o: file.o
	dmd main.d

bin/file.o:
	g++ -c file.cpp -o file.o



I get this error:

nico nical-netbook:~/Programmation/D/test binding$ make
g++    -c -o file.o file.cpp
g++ main.o file.o -o test
/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In
function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115:
undefined reference to `main'
main.o: In function `no symbol':
main.d:(.text+0x8): undefined reference to `_Dmodule_ref'
collect2: ld returned 1 exit status
make: *** [all] Erreur 1
nico nical-netbook:~/Programmation/D/test binding$

thanks in advance

Nico
Mar 08 2010
parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
Nical wrote:
 Hi, I'm trying to use C/C++ functions in a D program but I cant get
 it to compile.
 I tried the exemple of digital mars's website:
 
 // ---- file.cpp
 #include <iostream>
 using namespace std;
 int foo(int i, int j, int k)
 {
     cout << "i = " << i << endl;
     cout << "j = " << j << endl;
     cout << "k = " << k << endl;
     return 7;
 }
 
 // ---- main.d
 extern (C++) int foo(int i, int j, int k);
 void main()
 {
     foo(1,2,3);
 }
 
 and a makefile that might not be correct now that i've experimented
 various things on it:
 
 OBJ= main.o file.o
 
 all: ${OBJ}
 	g++ ${OBJ}
 
 bin/main.o: file.o
 	dmd main.d
 
 bin/file.o:
 	g++ -c file.cpp -o file.o
 
 
 
 I get this error:
 
 nico nical-netbook:~/Programmation/D/test binding$ make
 g++    -c -o file.o file.cpp
 g++ main.o file.o -o test
 /usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In
 function `_start':
 /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115:
 undefined reference to `main'
 main.o: In function `no symbol':
 main.d:(.text+0x8): undefined reference to `_Dmodule_ref'
 collect2: ld returned 1 exit status
 make: *** [all] Erreur 1
 nico nical-netbook:~/Programmation/D/test binding$
 
 thanks in advance
 
 Nico

The problem is that the linker doesn't find the main() function. I think the following is what happens when you write a D program and compile it with DMD: 1. You write a D-style main() function. 2. The D compiler automatically includes in the final executable a C-style main() function that calls your D main(). This is the actual starting point for your program, and the one the linker is looking for. g++ unfortunately doesn't include the C main() function, because it doesn't know where to find it. There are thus two ways to solve your problem: a) Use DMD to link the files. You then have to explicitly pass it the name of the C++ standard library as a linker option: g++ -c file.cpp -o file.o dmd -L-lstdc++ main.d file.o b) Use g++ to link the files. You then have to explicitly pass it the name and location of the D standard library and libpthread: g++ -c file.cpp -o file.o dmd -c main.d g++ -L/path/to/D/library -lphobos2 -lpthread main.o file.o I recommend a). Less typing. :) -Lars
Mar 08 2010