2

问候 StackOverflow 社区,

我有一个 SWIG 项目,它为 C++ 静态存档库生成 Ruby C API 包装器。我在 64 位 Linux 下生成和编译代码没有任何问题(在 Fedora 和 Debian 下测试)。不幸的是,存档的 OS X 版本只有 32 位。我在 OS X 上构建了一个 32 位版本的 Ruby,以便生成、编译和链接将用作 Ruby C 本机扩展的共享对象:

$ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [i386-darwin11]

所有对象都使用-m32标志编译:

$ g++ -m32 -Wall -g -c -fPIC -rdynamic ../src/CPPExampleWrapper.cpp ../src/CPPExampleException.cpp ../src/CPPExampleObject.cpp ../src/CPPExampleCallbackFunctor.cpp -I../include/ -I../lib/include/

并相应地链接:

g++ -m32 -shared -fPIC -o ../lib/cppexample.so ./bin/CPPExampleWrapper.o ../qa_cpp_utils/bin/CPPExampleObject.o ../qa_cpp_utils/bin/CPPExampleCallbackFunctor.o ../qa_cpp_utils/lib/libcppexamplearchive.a -framework CoreFoundation -lpthread

在链接时,我收到一条错误消息,指出 CPPExampleWrapper.o 中引用的符号对于体系结构 i386 未定义:

Undefined symbols for architecture i386:
  "_rb_big2long", referenced from:
      SWIG_AUX_NUM2LONG(unsigned long*)in CPPExampleWrapper.o
  "_rb_big2ulong", referenced from:
      SWIG_AUX_NUM2ULONG(unsigned long*)in CPPExampleWrapper.o
  "_rb_cFalseClass", referenced from:
      _rb_class_of in CPPExampleWrapper.o
  "_rb_cFixnum", referenced from:
      _rb_class_of in CPPExampleWrapper.o
  "_rb_cNilClass", referenced from:
      _rb_class_of in CPPExampleWrapper.o
  "_rb_cObject", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
      _SWIG_Ruby_SetModule in CPPExampleWrapper.o
      _SWIG_Ruby_define_class in CPPExampleWrapper.o
  "_rb_cSymbol", referenced from:
      _rb_class_of in CPPExampleWrapper.o
  "_rb_cTrueClass", referenced from:
      _rb_class_of in CPPExampleWrapper.o
  "_rb_check_type", referenced from:
      _SWIG_Ruby_GetModule in CPPExampleWrapper.o
      _SWIG_Ruby_ConvertPtrAndOwn in CPPExampleWrapper.o
  "_rb_const_get", referenced from:
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
  "_rb_data_object_alloc", referenced from:
      _SWIG_Ruby_SetModule in CPPExampleWrapper.o
      _SWIG_Ruby_NewClassInstance in CPPExampleWrapper.o
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
  "_rb_define_alloc_func", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_define_class", referenced from:
      getNullReferenceError()      in CPPExampleWrapper.o
      getObjectPreviouslyDeletedError()      in CPPExampleWrapper.o
      _SWIG_Ruby_SetModule in CPPExampleWrapper.o
  "_rb_define_class_under", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
      _SWIG_Ruby_define_class in CPPExampleWrapper.o
  "_rb_define_const", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_define_method", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_define_module", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
      _SWIG_Ruby_InitRuntime in CPPExampleWrapper.o
  "_rb_define_module_function", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_define_readonly_variable", referenced from:
      _SWIG_Ruby_SetModule in CPPExampleWrapper.o
  "_rb_define_singleton_method", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_eArgError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
      _wrap_new_JobOrder(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_src_location_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_src_location_get(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_dst_location_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_dst_location_get(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_xfer_params_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      ...
  "_rb_eFatal", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eIOError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eIndexError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eNoMemError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eRangeError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eRuntimeError", referenced from:
      getNullReferenceError()      in CPPExampleWrapper.o
      getObjectPreviouslyDeletedError()      in CPPExampleWrapper.o
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eSyntaxError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eTypeError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_eZeroDivError", referenced from:
      SWIG_Ruby_ErrorType(int)   in CPPExampleWrapper.o
  "_rb_funcall", referenced from:
      _SWIG_RubyRemoveTracking in CPPExampleWrapper.o
  "_rb_gv_get", referenced from:
      _SWIG_Ruby_GetModule in CPPExampleWrapper.o
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
  "_rb_gv_set", referenced from:
      _SWIG_Ruby_GetModule in CPPExampleWrapper.o
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
  "_rb_hash_aref", referenced from:
      _SWIG_RubyInstanceFor in CPPExampleWrapper.o
  "_rb_hash_aset", referenced from:
      _SWIG_RubyAddTracking in CPPExampleWrapper.o
  "_rb_hash_new", referenced from:
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
  "_rb_inspect", referenced from:
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_int2big", referenced from:
      _SWIG_RubyPtrToReference in CPPExampleWrapper.o
      _SWIG_RubyObjectToReference in CPPExampleWrapper.o
      SWIG_From_int(int)   in CPPExampleWrapper.o
  "_rb_intern", referenced from:
      _SWIG_Ruby_InitRuntime in CPPExampleWrapper.o
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
  "_rb_iv_get", referenced from:
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
      _SWIG_Ruby_MangleStr in CPPExampleWrapper.o
  "_rb_iv_set", referenced from:
      _SWIG_Ruby_NewClassInstance in CPPExampleWrapper.o
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
  "_rb_ivar_get", referenced from:
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
  "_rb_ivar_set", referenced from:
      _SWIG_RubyInitializeTrackings in CPPExampleWrapper.o
  "_rb_num2long", referenced from:
      _SWIG_RubyReferenceToObject in CPPExampleWrapper.o
      SWIG_AUX_NUM2LONG(unsigned long*)in CPPExampleWrapper.o
  "_rb_num2ulong", referenced from:
      SWIG_AUX_NUM2ULONG(unsigned long*)in CPPExampleWrapper.o
  "_rb_obj_classname", referenced from:
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_obj_is_kind_of", referenced from:
      _SWIG_Ruby_ConvertPtrAndOwn in CPPExampleWrapper.o
  "_rb_raise", referenced from:
      _wrap_new_JobOrder(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_src_location_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_src_location_get(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_dst_location_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_dst_location_get(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_xfer_params_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_JobOrder_xfer_params_get(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      ...
  "_rb_rescue", referenced from:
      SWIG_AsVal_unsigned_SS_long(unsigned long, unsigned long*)in CPPExampleWrapper.o
      SWIG_AsVal_long(unsigned long, long*)in CPPExampleWrapper.o
  "_rb_str_append", referenced from:
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_str_cat", referenced from:
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_str_cat2", referenced from:
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_str_new", referenced from:
      SWIG_FromCharPtrAndSize(char const*, unsigned long)in CPPExampleWrapper.o
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_str_new_cstr", referenced from:
      _SWIG_Ruby_NewClassInstance in CPPExampleWrapper.o
      _SWIG_Ruby_NewPointerObj in CPPExampleWrapper.o
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_string_value_ptr", referenced from:
      _wrap_XferParams_exclude_patterns_set(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _wrap_CPPTransfer_SetExcludePattern(int, unsigned long*, unsigned long)in CPPExampleWrapper.o
      _SWIG_Ruby_MangleStr in CPPExampleWrapper.o
      SWIG_AsCharPtrAndSize(unsigned long, char**, unsigned long*, int*)in CPPExampleWrapper.o
      Ruby_Format_TypeError(char const*, char const*, char const*, int, unsigned long)in CPPExampleWrapper.o
  "_rb_uint2big", referenced from:
      SWIG_From_unsigned_SS_long(unsigned long)in CPPExampleWrapper.o
  "_rb_undef_alloc_func", referenced from:
      _Init_CPPExample in CPPExampleWrapper.o
  "_rb_undef_method", referenced from:
      _SWIG_Ruby_define_class in CPPExampleWrapper.o
ld: symbol(s) not found for architecture i386
collect2: ld returned 1 exit status

也许并非如此巧合,所有未定义的符号都是 Ruby C API 的成员。我在编译或链接时遗漏了什么吗?或者,为便于生成此扩展而创建的 Ruby 的 32 位 OS X 构建可能有问题?

非常感谢任何反馈。

干杯,

詹姆士

4

1 回答 1

0

在这种情况下,问题在于 Xcode 安装了 64 位版本的 Ruby 并将其放在系统路径上。为了解决这个问题,我必须在链接行明确指定 32 位版本的 Ruby 二进制文件的路径:

g++ -arch i386 -shared -fPIC -o ../lib/cppexample.so ./bin/CPPExampleWrapper.o ../qa_cpp_utils/bin/CPPExampleObject.o ../qa_cpp_utils/bin/CPPExampleCallbackFunctor.o ../qa_cpp_utils/lib/libcppexamplearchive.a -L/PATH/TO/32BIT/RUBY -framework CoreFoundation -lpthread

这在 Linux 中不是问题,因为系统路径上的 Ruby 版本是为正在链接的体系结构构建的。

詹姆士

于 2013-03-15T19:41:49.420 回答