www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Linking doubts

reply Carotinho <carotinobg yahoo.it> writes:
Hi!
I have two linking questions

1) If I create a module, and then import it in another source file, how come
that a function defined in the module is "undefined", if I try to dmd only
the source file? I have to compile the module, and then link the source
file against the module. Is it right or I'm missing something?

2) Is it possible to make D functions available to a C program? I tried
linking the C source code and the D object altogether with phobos, but with
errors... what I'm supposed to do?

I thank you in advance:)

Carotinho
Nov 29 2004
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Carotinho wrote:
 Hi!
 I have two linking questions
 
 1) If I create a module, and then import it in another source file, how come
 that a function defined in the module is "undefined", if I try to dmd only
 the source file? I have to compile the module, and then link the source
 file against the module. Is it right or I'm missing something?
Importing a module gives you the ability to call functions in that module. However, the function code is only compiled in the *other* module. The C analogy would be that when you import a module, it's like #include-ing a header, while compiling a module is is like compiling a C file. So yes, you have to compile and link together all modules which include functions that you need.
 2) Is it possible to make D functions available to a C program? I tried
 linking the C source code and the D object altogether with phobos, but with
 errors... what I'm supposed to do?
You can make a D function callable from C like this: extern(C) int myFunction(<args>) { <code> } You can make a C function callable from D like this: extern(C) int myOtherFunction(<args>);
Nov 29 2004
parent reply Sebastian Beschke <s.beschke gmx.de> writes:
Russ Lewis wrote:
 
 You can make a C function callable from D like this:
     extern(C) int myOtherFunction(<args>);
 
And, just for clarifying, you need to link the appropriate object file in. -Sebastian
Nov 30 2004
parent Carotinho <carotinobg yahoo.it> writes:
Sebastian Beschke wrote:

 Russ Lewis wrote:
 
 You can make a C function callable from D like this:
     extern(C) int myOtherFunction(<args>);
 
And, just for clarifying, you need to link the appropriate object file in. -Sebastian
Thanks! I -ahem- misread the docs and thought that extern (C) was here just to link TO C :) Byez!
Nov 30 2004