在不同的文件夹中访问另一个Erlang模块

rt4zxlrg  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(174)

我有一个非常简单的疑问,但是我似乎在任何地方都找不到一个合适的解决方案。我有两个Erlang模块,module1.erlmodule2.erl。正如我的项目的提交指南所定义的,这两个模块属于不同的部分,因此分别在同一目录下的不同文件夹part1和part2中。结构如下:

src/
   part1/
       module1.erl
   part2/
       module2.erl

现在module2依赖于module1,并调用module1的各种方法作为module1:method()。当module1.erlmodule2.erl都在同一个文件夹中时,我能够实现全部功能,但现在它们在不同的文件夹中,我试图从part2文件夹运行module2.erl,我不知道如何允许module2编译和调用module1的方法。

wvt8vs2t

wvt8vs2t1#

因为仿真器正在使用中,所以当我们直接在module2的路径中使用默认选项启动仿真器时,module1的路径默认不在***代码路径***中。这可以使用...

1> code:get_path().
code:get_path().
[".","/usr/local/lib/erlang/lib/kernel-8.2/ebin",
 "/usr/local/lib/erlang/lib/stdlib-3.17/ebin",
 "/usr/local/lib/erlang/lib/xmerl-1.3.28/ebin",
 "/usr/local/lib/erlang/lib/wx-2.1.1/ebin",
 "/usr/local/lib/erlang/lib/tools-3.5.2/ebin",
 "/usr/local/lib/erlang/lib/tftp-1.0.3/ebin",
 "/usr/local/lib/erlang/lib/syntax_tools-2.6/ebin",
 "/usr/local/lib/erlang/lib/ssl-10.6/ebin",
 "/usr/local/lib/erlang/lib/ssh-4.13/ebin",
 "/usr/local/lib/erlang/lib/snmp-5.11/ebin",
 "/usr/local/lib/erlang/lib/sasl-4.1.1/ebin",
 "/usr/local/lib/erlang/lib/runtime_tools-1.17/ebin",
 "/usr/local/lib/erlang/lib/reltool-0.9/ebin",
 "/usr/local/lib/erlang/lib/public_key-1.11.3/ebin",
 "/usr/local/lib/erlang/lib/parsetools-2.3.2/ebin",
 "/usr/local/lib/erlang/lib/os_mon-2.7.1/ebin",
 "/usr/local/lib/erlang/lib/odbc-2.13.5/ebin",
 "/usr/local/lib/erlang/lib/observer-2.10.1/ebin",
 "/usr/local/lib/erlang/lib/mnesia-4.20.1/ebin",
 "/usr/local/lib/erlang/lib/megaco-4.2/ebin",
 "/usr/local/lib/erlang/lib/inets-7.5/ebin",
 "/usr/local/lib/erlang/lib/hipe-4.0.1/ebin",
 "/usr/local/lib/erlang/lib/ftp-1.1/ebin",
 "/usr/local/lib/erlang/lib/eunit-2.7/ebin",
 "/usr/local/lib/erlang/lib/et-1.6.5/ebin",
 "/usr/local/lib/erlang/lib/erts-12.2/ebin",
 "/usr/local/lib/erlang/lib/erl_interface-5.1/ebin",
 [...]|...]

这个列表中有.,但没有../part1,所以当我们编译时,在part2目录中,它失败了...

2> c(module1).
c(module1).
{error,non_existing}

有几种方法来解决这个问题,几个简单的可以...

  1. c("../part1/module1.erl"). .根据c文档

...Module可以是模块名称或原始程式档路径,可以有或没有.erl副档名...

在上面的选项中,我们使用了module1源文件的相对路径。
1.使用选项***-pa***调用erl,该选项会将part1的路径添加到erlang emulator的该会话的代码路径中。

part2$ erl -pa "../part1"
Erlang/OTP 25 [DEVELOPMENT] [erts-12.2] [source-c1ab4b5424] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]

Eshell V12.2  (abort with ^G)
1> code:get_path().
["../part1",".","/usr/local/lib/erlang/lib/kernel-8.2/ebin",
 "/usr/local/lib/erlang/lib/stdlib-3.17/ebin",
 "/usr/local/lib/erlang/lib/xmerl-1.3.28/ebin",
 "/usr/local/lib/erlang/lib/wx-2.1.1/ebin",
 "/usr/local/lib/erlang/lib/tools-3.5.2/ebin",
 "/usr/local/lib/erlang/lib/tftp-1.0.3/ebin",
 "/usr/local/lib/erlang/lib/syntax_tools-2.6/ebin",
 "/usr/local/lib/erlang/lib/ssl-10.6/ebin",
 "/usr/local/lib/erlang/lib/ssh-4.13/ebin",
 "/usr/local/lib/erlang/lib/snmp-5.11/ebin",
 "/usr/local/lib/erlang/lib/sasl-4.1.1/ebin",
 "/usr/local/lib/erlang/lib/runtime_tools-1.17/ebin",
 "/usr/local/lib/erlang/lib/reltool-0.9/ebin",
 "/usr/local/lib/erlang/lib/public_key-1.11.3/ebin",
 "/usr/local/lib/erlang/lib/parsetools-2.3.2/ebin",
 "/usr/local/lib/erlang/lib/os_mon-2.7.1/ebin",
 "/usr/local/lib/erlang/lib/odbc-2.13.5/ebin",
 "/usr/local/lib/erlang/lib/observer-2.10.1/ebin",
 "/usr/local/lib/erlang/lib/mnesia-4.20.1/ebin",
 "/usr/local/lib/erlang/lib/megaco-4.2/ebin",
 "/usr/local/lib/erlang/lib/inets-7.5/ebin",
 "/usr/local/lib/erlang/lib/hipe-4.0.1/ebin",
 "/usr/local/lib/erlang/lib/ftp-1.1/ebin",
 "/usr/local/lib/erlang/lib/eunit-2.7/ebin",
 "/usr/local/lib/erlang/lib/et-1.6.5/ebin",
 "/usr/local/lib/erlang/lib/erts-12.2/ebin",
 [...]|...]
2> c(module2).
{ok,module2}
3> c(module1).
Recompiling /home/nalin/source/erlang/part2/../part1/module1.erl
{ok,module1}
4> module2:exec().
"Module2"
5> module1:exec().
"Module1"
6>

我希望这些应该足以让你继续下去。另外,你必须抓住机会通读Compilation and Code Loading,对发生的事情有一些了解。

WYSIWYG***=〉***WHAT YOU SHOW IS WHAT YOU GET

相关问题