dlang 调用其他文件中的函数失败

dlang call to function in other file fails

我正在测试使用 D 代码调用其他文件中的函数 我的问题是我收到了我不理解的错误 在 server.d

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

并在 client.d

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

"dmd server.d" 呈现此输出错误

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

知道为什么我无法拨打电话吗? 哦,我在 OS X 10.9 只要函数与 main() 在同一个文件中,调用就可以工作 /一个

D 像 C++ 一样破坏所有符号名称。 除非模块名称匹配 1:1(您不应该这样做),否则诸如函数之类的符号将不会匹配。

如果您真的需要它,请切换到 extern(C),它会如您所愿。

尝试删除 "extern ..." 和 添加了 "import client; " 然后用 "dmd server.d client.d"

编译这两个文件

结论:要调用其他文件中的函数,需要在顶部导入每个文件,然后在同一行编译所有文件。

dmd server.d client.d