2011-05-16 61 views
4

我正在研究一些混合OCaml和C的代碼,在OCaml 3.12中引入了函數caml_release_runtime_system()caml_acquire_runtime_system()(它們在早期版本中被稱爲別的東西),但我想要兼容回到3.10如果可能,是否有#ifdef我可以使用它?我瀏覽了頭文件(在我的Debian系統上的/usr/lib/ocaml/caml),並且找不到可能的東西。謝謝!編譯時檢測OCaml版本C

UPDATE:這是我做過什麼

這是我做過什麼:

#if OCAML_VERSION_MINOR >= 12 
#include <caml/threads.h> 
#else 
#include <caml/signals.h> 
#endif 

#ifndef caml_acquire_runtime_system 
#define caml_acquire_runtime_system caml_leave_blocking_section 
#define caml_release_runtime_system caml_enter_blocking_section 
#endif 

回答

3

ocamloptocamlc二進制文件支持-vnum and -version switches用於獲取版本號:

-vnum or -version Print the version number of the compiler in short form (e.g. 3.11.0), then exit.

該開關在3.12.0和文檔中的示例文本中得到支持表明3.11.0也支持它。我沒有3.10.0方便,但nlucaroni(其OCaml富看起來比我更強)在評論中表示3.10.0確實有ocamplopt -version

所以,你可以添加這樣的事情你的Makefile:

OCAML_VERSION_MAJOR = `ocamlopt -version | cut -f1 -d.` 
OCAML_VERSION_MINOR = `ocamlopt -version | cut -f2 -d.` 
OCAML_VERSION_POINT = `ocamlopt -version | cut -f3 -d.` 

,然後使用-DOCAML_VERSION_MAJOR=$(OCAML_VERSION_MAJOR)-DOCAML_VERSION_MINOR=$(OCAML_VERSION_MINOR),這些信息傳遞給你的編譯器...

+2

這對3.11.2中的我不起作用。它在我的3.12.0版本中工作。 'ocamlopt -version'就是我在我的配置腳本中使用的,它在3.10.0到現在都有效。 – nlucaroni 2011-05-16 19:32:08

+0

嗯,它需要工作在OCaml 3.10到,以及任何以上的版本,我猜... – Gaius 2011-05-16 20:00:29

+1

@nlucaroni,@Gaius:我更新它使用ocamlopt -version,這是否適用於每個人3.10 ? – 2011-05-16 20:10:58

3

CAML/threads.h:

#define caml_acquire_runtime_system caml_leave_blocking_section 
#define caml_release_runtime_system caml_enter_blocking_section 

因此,只需將相同的行添加到您的C代碼(用#ifndef caml_acquire_runtime_system來保護),並使您的構建系統(和用戶)免於依賴外部實用程序和版本號。

+0

不錯!當我查看頭文件時,我確實看到了這些信息,但它從來沒有發生過我的工作 – Gaius 2011-05-17 08:56:52

+0

啊,但似乎是用3.11編譯我得到'error:caml/threads.h:No such file or directory'所以這不起作用 – Gaius 2011-05-19 08:23:19