dlang call to function in other file fails

2019-09-08 12:02发布

问题:

I'm testing to make calls to functions in other files using D code My problem is that I receive errors I don't understand in server.d

import std.stdio;
extern (D) void otherFunction();
main(){
otherFunction();}

and in client.d

import std.stdio;
void otherFunction(){ writeln("hello world");}

"dmd server.d" renders this output error

Undefined symbols for architecture x86_64:
  "_D6server13otherFunctionFZv", referenced from:
      __Dmain in server.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: linker exited with status 1

Any ideas why I can't make the call? Oh I'm on OS X 10.9 And the call work as long as the function is in the same file as main() /a

回答1:

D like C++ mangles all symbol names. Unless the module name matches 1:1 (which you shouldn't do) then a symbol such as a function won't match up.

If you really really need this, switch over to extern(C) which work as you're expecting.



回答2:

tried removing "extern ..." and added "import client; " then compiled the two files together with "dmd server.d client.d"

Conclusion: To call functions in other files it is needed to import each file at the top and then compile all files in same line.

dmd server.d client.d


标签: function d