如何使用自定义模块进行编译

发布于 2024-12-04 09:40:47 字数 973 浏览 0 评论 0原文

我正在尝试用 2 个 .ml 编译一个项目,其中一个是遵循此格式的模块

module Mymodule =  
  struct  
  ...  
  end;; 

我还为 myModule 创建了一个 .mli

module Mymodule =  
  sig  
  ...  
  end 

但现在当我在 main.ml 中调用 Mymodule.myfunction 时,我得到 "Unbound值 Mymodule.myfunction"

这是我的 makefile(我也有标准的 OcamlMakeFile):

RESULT= result  
SOURCES= Mymodule.ml main.ml  
LIBS= bigarray sdl sdlloader sdlttf sdlmixer

INCDIRS= +sdl

include OCamlMakefile

我搜索并尝试了一些东西,但没有任何效果:(


感谢您的回答,我按照您链接的教程进行操作,但现在我遇到了 SDL 链接问题:

File "testsdl_2.ml", line 1, characters 0-1:
Error: No implementations provided for the following modules:
         Sdl referenced from testsdl_2.cmx
         Sdlloader referenced from testsdl_2.cmx
         Sdlvideo referenced from testsdl_2.cmx

并且我' m 使用这一行来编译:

ocamlopt -I +sdl -o testsdl mymodule.cmx main.ml

I'm trying to compile a project with 2 .ml and one of them is a module follwing this format

module Mymodule =  
  struct  
  ...  
  end;; 

I also created a .mli for myModule

module Mymodule =  
  sig  
  ...  
  end 

But now when I call Mymodule.myfunction in main.ml, I get "Unbound value Mymodule.myfunction".

Here is my makefile (I also have the standard OcamlMakeFile) :

RESULT= result  
SOURCES= Mymodule.ml main.ml  
LIBS= bigarray sdl sdlloader sdlttf sdlmixer

INCDIRS= +sdl

include OCamlMakefile

I searched and tried some things but nothing is working :(


Thanks for your answer, I followed the tutorial you linked but now I've a problem with SDL linking:

File "testsdl_2.ml", line 1, characters 0-1:
Error: No implementations provided for the following modules:
         Sdl referenced from testsdl_2.cmx
         Sdlloader referenced from testsdl_2.cmx
         Sdlvideo referenced from testsdl_2.cmx

and I'm using this line to compile:

ocamlopt -I +sdl -o testsdl mymodule.cmx main.ml

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-12-11 09:40:47

每个 ml 源文件已经代表一个模块(名称等于文件名)。
仔细阅读有关模块的 ocaml 教程

Each ml source file already represents a module (with the name equal to the name of the file).
Read ocaml tutorial on modules carefully.

谈下烟灰 2024-12-11 09:40:47

为了扩大 ygrek 的答案,通过在名为 Mymodule.ml 的文件中声明名为 Mymodule 的模块,您将创建一个名为 Mymodule.Mymodule 的模块。您很可能只想删除 .ml 和 .mli 文件中的 module Mymodule 包装器,然后事情就会按您的预期进行。本质上,OCaml 为每个源文件免费提供一层模块包装。

To enlarge a little on ygrek's answer, by declaring a module named Mymodule inside a file named Mymodule.ml, you are creating a module named Mymodule.Mymodule. Most likely you just want to remove the module Mymodule wrappers in the .ml and .mli files and then things will work as you expect. In essence, OCaml gives you one layer of module wrapping for free with each source file.

分分钟 2024-12-11 09:40:47

我找到了解决方案:)

您必须更改所使用的库中的正确文件“META”。就我而言,必须通过添加以下行来更改库 odepack 中的 META 文件

requires = "bigarray"

之后,必须对 Makefile 进行细微修改。该行

LIBS = unix str bigarray

修改为

LIBS = str

错误

File "_none_", line 1, characters 0-1:
Error: Files /usr/lib/ocaml/unix.cmxa and /usr/lib/ocaml/unix.cmxa
   both define a module named Unix

此修改避免了当我们定义同一个库两次或多次时出现的 。在我的例子中,Bigarray 库足以包含 Unix 库。

I found the solution :)

You have to change the right files "META" in the used libraries. In my case, the META file in the library odepack have to be changed by adding the following line

requires = "bigarray"

After that, a minor modification in the Makefile have to be done. The line

LIBS = unix str bigarray

is modified to

LIBS = str

This modification avoids the error

File "_none_", line 1, characters 0-1:
Error: Files /usr/lib/ocaml/unix.cmxa and /usr/lib/ocaml/unix.cmxa
   both define a module named Unix

that occurs when we define the same library twice or more. The library Bigarray in my case is sufficient to include the Unix library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文