OCaml应该在Windows中通过自定义链接编译(通过MinGW)? [英] Should OCaml compilation with custom linking work in Windows (via MinGW)?

查看:115
本文介绍了OCaml应该在Windows中通过自定义链接编译(通过MinGW)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编译一个与C代码接口的OCaml程序,使用基于MinGW的GCC,并使用单独的编译(GCC生成 .o ,然后<$ c

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



我使用Jonathan Protzenko的 OCaml on Windows安装程序以安装OCaml 4.02.1以及一个Cygwin shell(注意它使用本机windows OCaml编译器,而不是基于Cygwin的)。我使用 Nuwen的MinGW 安装了gcc(但在使用Strawberry Perl的gcc时遇到同样的问题)。



这是我的源代码:

C文件( tc.c ):

  #include< stdio.h> 
#includecaml / mlvalues.h

value print(未使用的值){
printf(你好,来自C\\\
);
返回Val_unit;

OCaml文件( t.ml

  external print:unit  - > unit =print

let()=
Printf.printfplatform:%s\\\
(Sys.os_type);
print();

下面的例子很好:

 和@ win7 $ ocamlopt t.ml tc.c -o t.exe 
和@ win7 $ ./t.exe
平台:Win32
hello从C

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

 和@win7 $ gcc tc.c -c -I c:/ OCaml / lib -o tc.o 
和@ win7 $ ocamlopt t.ml tc.o -o t.exe
**无法解析tc.o的符号:
放置
**致命错误:不支持的重定位种类0004 for put in tc.o
文件caml_startup,第1行:
错误:链接
时出错

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



我想知道是否可以通过给gcc / ocamlc / ocamlopt提供正确的参数来快速解决这个问题,或者它是目前Windows上OCaml本地编译的限制。



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

解决方案

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

  $ ocamlc -config 
版本: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编译器是通过Cygwin 32位环境构建的,其中 x86_64的-W64-的mingw32-GCC 。这同样适用于链接器和 ranlib 。由于您可以使用 ocamlopt 来编译C,所以必须在您的环境中安装相同的C编译器。



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


I want to compile an OCaml program interfacing with C code, using a MinGW-based GCC, and using separate compilation (GCC produces the .o, then ocamlopt produces the final executable).

It's not clear to me if (1) this should work on Windows and, if so, (2) which command-line arguments are necessary.

I'm using Jonathan Protzenko's OCaml on Windows installer to install OCaml 4.02.1 along with a Cygwin shell (note that it uses a native windows OCaml compiler, not a Cygwin-based one). I installed gcc using Nuwen's MinGW (but had the same issue when using Strawberry Perl's gcc).

Here's my source code:

C file (tc.c):

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

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

OCaml file (t.ml):

external print : unit -> unit = "print"

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

The following works just fine:

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

However, if I use a .o instead of a .c, it doesn't work:

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

Both versions work fine on Linux.

I wonder if it's just some silly mistake that I can quickly fix by giving the right arguments to gcc/ocamlc/ocamlopt, or if it's a current limitation of OCaml's native compilation on Windows.

Edit: camlspotter identified the cause, so in retrospect, I did not need Nuwen's MinGW at all. OCaml on Windows already includes a MinGW-based C compiler, except that it is called i686-w64-mingw32-gcc and not gcc.

解决方案

You are probably using a wrong C compiler or without appropriate options. The best way is to use the same C compiler + options used to build OCaml. You can check it by 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
...

For example, the above shows that my OCaml compiler is built over Cygwin 32 bit environment with x86_64-w64-mingw32-gcc. The same applies for the linker and ranlib. Since you can compile C with OCaml code with ocamlopt, the same C compiler must be already installed in your environment.

Building OCaml compiler by yourself to make sure the same C compiler is used both for C and OCaml may be the best way to avoid this sort of C compiler mismatch.

这篇关于OCaml应该在Windows中通过自定义链接编译(通过MinGW)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆