使用 opam 为 Z3 安装 ocaml API
Installing ocaml API for Z3 using opam
我想在我的 OCaml 程序中使用 Z3。使用 opam,我做到了
$ opam install z3
$ eval $(opam env)
然后尝试用
编译
$ ocamlfind ocamlopt -o main -package z3 -linkpkg main.ml
我得到的是数千个 In function foo undefined reference to bar
的巨大转储,从
开始
/home/andrepd/.opam/4.06.1+flambda/lib/z3/libz3-static.a(api_datatype.o): In function `mk_datatype_decl':
api_datatype.cpp:(.text+0x4bf): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x522): undefined reference to `__cxa_throw'
api_datatype.cpp:(.text+0x57b): undefined reference to `__cxa_free_exception'
api_datatype.cpp:(.text+0x58f): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x5f9): undefined reference to `__cxa_throw'
api_datatype.cpp:(.text+0x61f): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x68b): undefined reference to `__cxa_throw'
...
以
结尾
binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x1ab): undefined reference to `__cxa_allocate_exception'
binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x205): undefined reference to `__cxa_throw'
binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x226): undefined reference to `__cxa_free_exception'
/home/andrepd/.opam/4.06.1+flambda/lib/z3/libz3-static.a(binary_heap_upair_queue.o): In function `_GLOBAL__sub_I_binary_heap_upair_queue.cpp':
binary_heap_upair_queue.cpp:(.text.startup+0xc): undefined reference to `std::ios_base::Init::Init()'
binary_heap_upair_queue.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.
我做错了什么?作为记录,我使用的是 ocamlbuild,
$ ocamlbuild -use-ocamlfind -cflag '-linkpkg' main.native
_tags
文件中的 和 true: package(z3)
。但是像上面那样调用普通的 ocamlfind 会给出相同的输出。
版本:编译器:4.06.1 w/ flambda,opam:2.0.0,z3:4.8.4。
TL;DR;包裹坏了。下面是修复程序和一些解决方法,但一般来说,此类问题应发布到相应的问题跟踪器。因此,请考虑打开一个问题报告或带有修复的拉取请求。
那些 linker 错误表明缺少 C++ 标准库中的符号。由于 OCaml 使用 C linker to link 最终产品,默认情况下它不会通过 C++ 标准库。当然,一个正确制作的包应该为我们做到这一点1,但我们仍然可以通过-cclib -lstdc++
手动完成(如果你有libstdc++,否则使用-lc++
).
ocamlfind ocamlopt -linkpkg -cclib -lstdc++ -package z3 example.ml -o exe
对于 ocamlbuild
,您可以使用 cclib(x)
参数化标签,例如
<example.ml>: cclib(-lstdc++)
沙丘用户注意事项
TL;DR;如果你正在使用 dune
那么你仍然需要添加 (flags (-cclib -lstdc++))
到你的 library/executable 节,因为 dune 忽略了 linkopts
(以及 META 文件的许多其他字段)。
说来话长。 META 文件规范由 findlib 库定义和实现。沙丘构建系统没有使用 findlib,而是 re-implemented findlib 的一个小子集,其中缺少许多功能,即像 linkopts
和谓词这样的字段。这就是为什么你仍然需要添加这个字段,尽管事实上 META 规定了它。至少截至 2019 年 10 月。
1 提供的 META 文件包含伪造的
linkopts = "-cclib -L/usr/lib"
哪个 (a) 没有意义,因为 -L
不是 linker 选项,而是编译器选项,并且 (b) 没用,因为 /usr/lib
是通常在搜索路径中。
正确的选项应该是:
linkopts = "-cclib -lstdc++"
您可以直接编辑文件,它位于$(ocamlfind query z3)/META
。
请考虑向 OPAM 包或(理想情况下)向 z3 提交修复。
我想在我的 OCaml 程序中使用 Z3。使用 opam,我做到了
$ opam install z3
$ eval $(opam env)
然后尝试用
编译$ ocamlfind ocamlopt -o main -package z3 -linkpkg main.ml
我得到的是数千个 In function foo undefined reference to bar
的巨大转储,从
/home/andrepd/.opam/4.06.1+flambda/lib/z3/libz3-static.a(api_datatype.o): In function `mk_datatype_decl':
api_datatype.cpp:(.text+0x4bf): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x522): undefined reference to `__cxa_throw'
api_datatype.cpp:(.text+0x57b): undefined reference to `__cxa_free_exception'
api_datatype.cpp:(.text+0x58f): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x5f9): undefined reference to `__cxa_throw'
api_datatype.cpp:(.text+0x61f): undefined reference to `__cxa_allocate_exception'
api_datatype.cpp:(.text+0x68b): undefined reference to `__cxa_throw'
...
以
结尾binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x1ab): undefined reference to `__cxa_allocate_exception'
binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x205): undefined reference to `__cxa_throw'
binary_heap_upair_queue.cpp:(.text._ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_[_ZN2lp23binary_heap_upair_queueIjE7dequeueERjS2_]+0x226): undefined reference to `__cxa_free_exception'
/home/andrepd/.opam/4.06.1+flambda/lib/z3/libz3-static.a(binary_heap_upair_queue.o): In function `_GLOBAL__sub_I_binary_heap_upair_queue.cpp':
binary_heap_upair_queue.cpp:(.text.startup+0xc): undefined reference to `std::ios_base::Init::Init()'
binary_heap_upair_queue.cpp:(.text.startup+0x13): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
File "caml_startup", line 1:
Error: Error during linking
Command exited with code 2.
我做错了什么?作为记录,我使用的是 ocamlbuild,
$ ocamlbuild -use-ocamlfind -cflag '-linkpkg' main.native
_tags
文件中的 和 true: package(z3)
。但是像上面那样调用普通的 ocamlfind 会给出相同的输出。
版本:编译器:4.06.1 w/ flambda,opam:2.0.0,z3:4.8.4。
TL;DR;包裹坏了。下面是修复程序和一些解决方法,但一般来说,此类问题应发布到相应的问题跟踪器。因此,请考虑打开一个问题报告或带有修复的拉取请求。
那些 linker 错误表明缺少 C++ 标准库中的符号。由于 OCaml 使用 C linker to link 最终产品,默认情况下它不会通过 C++ 标准库。当然,一个正确制作的包应该为我们做到这一点1,但我们仍然可以通过-cclib -lstdc++
手动完成(如果你有libstdc++,否则使用-lc++
).
ocamlfind ocamlopt -linkpkg -cclib -lstdc++ -package z3 example.ml -o exe
对于 ocamlbuild
,您可以使用 cclib(x)
参数化标签,例如
<example.ml>: cclib(-lstdc++)
沙丘用户注意事项
TL;DR;如果你正在使用 dune
那么你仍然需要添加 (flags (-cclib -lstdc++))
到你的 library/executable 节,因为 dune 忽略了 linkopts
(以及 META 文件的许多其他字段)。
说来话长。 META 文件规范由 findlib 库定义和实现。沙丘构建系统没有使用 findlib,而是 re-implemented findlib 的一个小子集,其中缺少许多功能,即像 linkopts
和谓词这样的字段。这就是为什么你仍然需要添加这个字段,尽管事实上 META 规定了它。至少截至 2019 年 10 月。
1 提供的 META 文件包含伪造的
linkopts = "-cclib -L/usr/lib"
哪个 (a) 没有意义,因为 -L
不是 linker 选项,而是编译器选项,并且 (b) 没用,因为 /usr/lib
是通常在搜索路径中。
正确的选项应该是:
linkopts = "-cclib -lstdc++"
您可以直接编辑文件,它位于$(ocamlfind query z3)/META
。
请考虑向 OPAM 包或(理想情况下)向 z3 提交修复。