【问题标题】:dlang call to function in other file failsdlang 调用其他文件中的函数失败
【发布时间】:2016-12-21 20:00:03
【问题描述】:

我正在测试使用 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() 在同一个文件中,调用就可以工作 /a

【问题讨论】:

    标签: function d


    【解决方案1】:

    尝试删除“extern ...”和 添加了“导入客户端;” 然后将这两个文件与“dmd server.d client.d”一起编译

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

    dmd server.d client.d
    

    【讨论】:

    • 是的,这是 DMD 编译器的常识。您必须指定要编译的 ALL 文件。
    【解决方案2】:

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

    如果您真的需要这个,请切换到extern(C),它会按您的预期工作。

    【讨论】:

    • 所以你理查德的意思是 D 不能处理函数在不同文件中的函数调用,除非使用 extern(C) 方式。
    • 这是错误信息。找不到符号,因为他没有告诉 DMD 编译文件。你必须给 DMD 所有它必须编译的文件。
    猜你喜欢
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多