OCaml编译与自定义链接是否应该在Windows中工作(通过MinGW)?

时间:2015-08-19 09:35:14

标签: windows gcc mingw ocaml

我想编译一个与C代码接口的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提供正确的参数来快速修复,或者它是否是当前对Windows上OCaml本机编译的限制。

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

1 个答案:

答案 0 :(得分:2)

您可能使用了错误的C编译器或没有适当的选项。最好的方法是使用相同的C编译器+选项来构建OCaml。您可以按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编译器不匹配的最佳方法。