c++ - 使用 swig 到 ocaml 的奇怪重命名行为

标签 c++ ocaml swig

我观察到奇怪的重命名行为 我将 swig 应用于我复制的 ocaml 代码 在 swig 官方示例代码(https://github.com/swig/swig/blob/master/Examples/ocaml/std_vector)上:

例子.h

#include <vector>
#include <stdexcept>
#include <numeric>

double average(std::vector<int> v) {
    # instead of calculating the average,
    # throw an invalid_argument
    throw std::invalid_argument("test");
    return 0;
}

example.i(与上述链接中的相同)

%module example

%{
#include "example.h"
%}

%include stl.i
/* instantiate the required template specializations */
%template(IntVector)    std::vector<int>;
%template(DoubleVector) std::vector<double>;

/* Let's just grab the original header file here */
%include "example.h"

这带来了:

➜  std_vector git:(master) ✗ make
/Library/Developer/CommandLineTools/usr/bin/make -f ../../Makefile 
SRCDIR='' SRCS='' \
SWIG_LIB_DIR='../../../Lib' SWIGEXE='../../../swig' \
PROGFILE='runme.ml' TARGET='example' INTERFACE='example.i' \
ocaml_static_cpp
rm -rf swig.mli swig.ml swigp4.ml && env SWIG_LIB=../../../Lib  ../../../swig -ocaml -co swig.mli 2>/dev/null && env SWIG_LIB=../../../Lib  ../../../swig -ocaml -co swig.ml 2>/dev/null && env SWIG_LIB=../../../Lib  ../../../swig -ocaml -co swigp4.ml 2>/dev/null &&  ocamlc -c swig.mli &&  ocamlc -c swig.ml &&  ocamlc -I ` camlp4 -where` -pp "camlp4o pa_extend.cmo q_MLast.cmo" -c swigp4.ml
env SWIG_LIB=../../../Lib  ../../../swig -ocaml -c++  -o 
example_wrap.cxx example.i
cp example_wrap.cxx example_wrap.c
ocamlc -cc 'g++ -Wno-write-strings' -g -c -ccopt -g -ccopt "-xc++ " 
example_wrap.c
ocamlc -g -c example.mli
ocamlc -g -c example.ml
(some warnings)
false ||  ocamlc -g -ccopt -g -cclib -g -custom -o example swig.cmo 
example.cmo runme.cmo example_wrap.o   -cclib "" -cc 'g++ -Wno-write-strings'
clang: warning: treating 'c' input as 'c++' when in C++ mode, this 
behavior is deprecated [-Wdeprecated]
Undefined symbols for architecture x86_64:
 "std::caml_invalid_argument::~caml_invalid_argument()", referenced from:
  average(std::__1::vector<int, std::__1::allocator<int> >) in example_wrap.o
  "typeinfo for std::caml_invalid_argument", referenced from:
  average(std::__1::vector<int, std::__1::allocator<int> >) in 
example_wrap.o
  "vtable for std::caml_invalid_argument", referenced from:
      average(std::__1::vector<int, std::__1::allocator<int> >) in 
example_wrap.o
  NOTE: a missing vtable usually means the first non-inline virtual 
member function has no definition.
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see 
invocation)
File "_none_", line 1:
Error: Error while building custom runtime system
make[1]: *** [ocaml_static_cpp] Error 2

问题是,如果某些名称 (invalid_argument) 与在带有“caml_”前缀的 ocaml C 接口(interface)中预定义的名称 (caml_invalid_argument) 一致,看起来前者的所有出现在编译期间的某些地方被后者替换. 我用不同的名称(初始化和 caml_initialize)和不同的环境(带有 clang 或 g++ 的 macOS Sierra 和 ubuntu 14.10 和 g++)观察到相同的问题。 这是 swig 或 ocaml 中的错误吗?

最佳答案

有一个名为 caml/compatibility.h 的文件,其定义完全如下:

. . .
#define failwith caml_failwith
#define invalid_argument caml_invalid_argument
. . .

也许它不应该被包括在内?

关于c++ - 使用 swig 到 ocaml 的奇怪重命名行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694747/

相关文章:

Python 中的 C++ API - SWIG、重新设计或消息传递

c++ - 如何在从原始图像中提取的子图像中执行模板匹配过程并在原始图像中显示结果

string - 在 OCaml 中将字符转换为字符串

ocaml - OCaml 3.12 : What kinds of things will they make easier (or possible)? 中的第一类模块

ocaml嵌套模块: the implementation ml does not match the interface cmi

javascript - 将 JavaScript 数组高效地传递给 c/c++ 库并返回

c++ - SWIG:向生成的 .py 文件添加注释

c++ - AIX unix 服务器上的 g++ 编译错误

c++ - 将 WPARAM 传递到 DragQueryFile 不兼容?

c++ - 声明一个新节点但不等于 NULL