dmd 链接器 (OPTLINK) 给出错误 42:使用 extern 时符号未定义
链接以下两个文件会出现链接错误:
ad:
import std.stdio;
extern string test ();
void main() {
writeln(test());
readln();
}
bd:
string test () {
return "hello";
}
我得到的错误是:
Error 42: Symbol Undefined _D1a4testFZAya`
---errorlevel 1
出了什么问题?
____________< /em>_________ __________________ __________
编辑:这是正确的方法:
ad:
import std.stdio;
import b;
void main() {
writeln("some_var from Module b: \"", b.some_var, "\"");
}
bd:
public string some_var = "Hello, world!";
//you can also use static module constructors to set your vars
static this() {
some_var ~= " -- How are you?";
}
该代码由 Joshua Reusch 在 针对初学者的优秀 D 论坛在 digitalmars.com 网站上。
Linking the following two files gives me a link-error:
a.d:
import std.stdio;
extern string test ();
void main() {
writeln(test());
readln();
}
b.d:
string test () {
return "hello";
}
the error I get is:
Error 42: Symbol Undefined _D1a4testFZAya`
---errorlevel 1
What is wrong ?
_________________________________________________
Edit: this is the right way to do it:
a.d:
import std.stdio;
import b;
void main() {
writeln("some_var from Module b: \"", b.some_var, "\"");
}
b.d:
public string some_var = "Hello, world!";
//you can also use static module constructors to set your vars
static this() {
some_var ~= " -- How are you?";
}
That code was kindly provided by Joshua Reusch in the excellent D forum for beginners in the digitalmars.com site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的
ad
修改为:extern
是一个链接属性,主要用于指定给定函数使用的调用约定(通常是 C某些库中的函数)。有关extern
和其他属性的更多信息,请访问:http://www. d-programming-language.org/attribute.html。如果你只有D源文件,那确实不需要extern。但是,如果您混合使用 C 或 C++ 和 D 代码,则肯定必须使用它。Modify your
a.d
to:extern
is a linkage attribute and is mostly used to specify what calling convention to use for the given function (typically a C function in some library). More aboutextern
and other attributes here: http://www.d-programming-language.org/attribute.html . If all you have are D source files, there is really no need for extern. However, if you mix C or C++ and D code, you will definitely have to use it.