windows - 带有自定义链接的 OCaml 编译是否应该在 Windows 中工作(通过 MinGW)?

标签 windows gcc mingw ocaml

我想编译一个与 C 代码接口(interface)的 OCaml 程序,使用基于 MinGW 的 GCC,并使用单独的编译(GCC 生成 .o,然后 ocamlopt 生成最终的可执行文件)。

我不清楚 (1) 这是否适用于 Windows,如果适用,(2) 哪些命令行参数是必需的。

我正在使用 Jonathan Protzenko 的 OCaml on Windows installer安装 OCaml 4.02.1 和 Cygwin shell(注意它使用原生 Windows OCaml 编译器,而不是基于 Cygwin 的编译器)。我使用 Nuwen's MinGW 安装了 gcc (但在使用 Strawberry Perl 的 gcc 时遇到了同样的问题)。

这是我的源代码:

C 文件(tc.c):

#include <stdio.h>
#include "caml/mlvalues.h"

value print(value unused) {
  printf("hello from C\n");
  return Val_unit;
}

OCaml 文件(t.ml):

external print : unit -> unit = "print"

let () =
  Printf.printf "platform: %s\n" (Sys.os_type);
  print ();

以下工作正常:

and@win7 $ ocamlopt t.ml tc.c -o t.exe
and@win7 $ ./t.exe
platform: Win32
hello from C

但是,如果我使用 .o 而不是 .c,它就不起作用:

and@win7 $ gcc tc.c -c -I c:/OCaml/lib -o tc.o
and@win7 $ ocamlopt t.ml tc.o -o t.exe
** Cannot resolve symbols for tc.o:
 puts
** Fatal error: Unsupported relocation kind 0004 for puts in tc.o
File "caml_startup", line 1:
Error: Error during linking

两个版本在 Linux 上都能正常工作。

我想知道这是否只是一些愚蠢的错误,我可以通过为 gcc/ocamlc/ocamlopt 提供正确的参数来快速修复,或者它是否是 OCaml 在 Windows 上的 native 编译的当前限制。

编辑:camlspotter查明了原因,所以回想起来,我根本不需要Nuwen的MinGW。 Windows 上的 OCaml 已经包含一个基于 MinGW 的 C 编译器,除了它被称为 i686-w64-mingw32-gcc 而不是 gcc

最佳答案

您可能使用了错误的 C 编译器或没有适当的选项。最好的方法是使用与构建 OCaml 相同的 C 编译器 + 选项。您可以通过 ocamlc -config 进行检查:

$ ocamlc -config
version: 4.02.3
standard_library_default: C:/ocamlmgw64/lib
standard_library: C:/ocamlmgw64/lib
standard_runtime: ocamlrun
ccomp_type: cc
bytecomp_c_compiler: x86_64-w64-mingw32-gcc -O -mms-bitfields -Wall -Wno-unused
bytecomp_c_libraries: -lws2_32
native_c_compiler: x86_64-w64-mingw32-gcc -O -mms-bitfields -Wall -Wno-unused
native_c_libraries: -lws2_32
native_pack_linker: x86_64-w64-mingw32-ld -r  -o 
ranlib: x86_64-w64-mingw32-ranlib
...

例如,上面显示我的 OCaml 编译器是在带有 x86_64-w64-mingw32-gcc 的 Cygwin 32 位环境上构建的。这同样适用于链接器和 ranlib。由于您可以使用 ocamlopt 使用 OCaml 代码编译 C,因此您的环境中必须已经安装了相同的 C 编译器。

自己构建 OCaml 编译器以确保为 C 和 OCaml 使用相同的 C 编译器可能是避免这种 C 编译器不匹配的最佳方法。

关于windows - 带有自定义链接的 OCaml 编译是否应该在 Windows 中工作(通过 MinGW)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32091729/

相关文章:

javascript - 当 Windows 进入休眠模式然后唤醒时,setTimeout 不起作用

c# - 我应该使用域名还是域 Controller 名称绑定(bind)到 Active Directory?

c - Linux 中 gcc 链接期间的多个定义错误

c++ - 链接使用不同版本的 GCC 构建的目标文件

C++,MingW,对 `libiconv' 的 undefined reference

windows - 在本地计算机上设置 AppFabric 缓存服务器 1.1 时出错

python - 安装 MySQL Python(在 Windows 7 上)

regex - Jenkins中的日志解析规则

c++ - 将 MinGW 从 4.4.1 更新到 4.7.1 后,SFML 项目将无法运行

c++ - mingw g++ 无法与库链接