gpt4 book ai didi

dmd 链接器 (OPTLINK) 给出错误 42 : Symbol Undefined when using extern

转载 作者:行者123 更新时间:2023-12-01 01:22:01 24 4
gpt4 key购买 nike

链接以下两个文件会给我一个链接错误:

广告:

import std.stdio;

extern string test ();

void main() {
writeln(test());
readln();
}

b.d:
string test () {
return "hello";
}

我得到的错误是:
Error 42: Symbol Undefined _D1a4testFZAya`

---errorlevel 1

怎么了 ?

_________________________________________________

编辑:这是正确的方法:

广告:
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?";
}

该代码由 Joshua Reusch 友情提供在 excellent D forum for beginners在 digitalmars.com 网站上。

最佳答案

修改您的 a.d到:

import std.stdio;
import b;

//extern string test ();

void main() {
writeln(test());
readln();
}
extern是一个链接属性,主要用于指定给定函数(通常是某个库中的 C 函数)使用的调用约定。更多关于 extern和其他属性在这里: http://www.d-programming-language.org/attribute.html .如果您拥有的只是 D 源文件,则真的不需要 extern。但是,如果您混合使用 C 或 C++ 和 D 代码,您肯定会使用它。

关于dmd 链接器 (OPTLINK) 给出错误 42 : Symbol Undefined when using extern,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8631476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com