简体   繁体   English

使用本地模块时出现 Mercury“未定义引用”编译错误

[英]Mercury “undefined reference” compilation error when using local module

I have a module exporting nat/1 to test/generate natural numbers:我有一个模块导出nat/1来测试/生成自然数:

:- module nat.

:- interface.

:- import_module int.

:- pred nat(int).
:- mode nat(in) is det.
:- mode nat(out) is multi.

:- implementation.

:- pragma promise_pure(nat/1).
nat(_::in).
nat(0::out).
nat(X::out) :- nat(Y), X = Y + 1.

and a main module in the same directory to try it out:和同一目录中的主模块进行尝试:

:- module main.

:- interface.
:- import_module io.

:- pred main(io__state::di, io__state::uo) is cc_multi.

:- implementation.
:- import_module nat.

main(!IO) :- nat(X), print(X, !IO).

I run mmc --make-int nat.m which successfully generates the interface files, but then when I run mmc main.m I get the following error:我运行mmc --make-int nat.m成功生成了接口文件,但是当我运行mmc main.m时出现以下错误:

/usr/bin/ld: main.o: in function `<predicate 'main'/2 mode 0>':
main.c:(.text+0x45): undefined reference to `<predicate 'nat.nat'/1 mode 1>'
collect2: error: ld returned 1 exit status

I'm using MMC version 20.06.1, on x86_64-pc-linux-gnu .version 20.06.1, on x86_64-pc-linux-gnu

Am I missing something obvious?我错过了一些明显的东西吗? Code improvements are also very welcome.代码改进也非常受欢迎。

After "mmc --make-int nat.m", the command you need to run is not "mmc main.m", but "mmc main.m nat.m".在“mmc --make-int nat.m”之后,需要运行的命令不是“mmc main.m”,而是“mmc main.m nat.m”。 The former compiles only main.m, while the latter also compiles nat.m.前者编译 main.m,而后者也编译 nat.m。 Both attempt to build an executable from the resulting object files, but the former will fail, since the definition of the "nat" predicate would be in the object file it does not generate.两者都尝试从生成的 object 文件构建可执行文件,但前者会失败,因为“nat”谓词的定义将在它不生成的 object 文件中。

In general, instead of trying to manage the creation of interface files, object files and executables manually, it is much easier to use an automated build system: either the mmake script, or mmc --make.通常,与其尝试手动管理接口文件、object 文件和可执行文件的创建,不如使用自动构建系统更容易:mmake 脚本或 mmc --make。

As for code improvements, I would suggest replacing io__state with just plain io, which is a lot shorter.至于代码改进,我建议用简单的 io 替换 io__state,它要短得多。 We added "io" as a synonym for the "state" type in io.m specifically to make this possible.我们在 io.m 中添加了“io”作为“状态”类型的同义词,以实现这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM