Swig Perl:错误的 ELF class

Swig Perl: wrong ELF class

我正在尝试使用 Swig 从 CXX 模块构建 Perl 模块。 有多个与此相关的指南:

  1. 带有 Perl 部分的通用 Swig tutorial
  2. Swig and C++ 指南
  3. Swig and Perl5 指南

我是 Swig 的新手,对 C(++) 不是很熟悉,但我已经能够按照 1:

中的教程编译我的模块

我创建了一个接口文件:

%module my_module

%{
    #include "case.h"
    #include "case.h"
    #include "lexindex.h"
    #include "intlist.h"
    #include "weight.h"
    #include "invindex.h"
    #include "winnow.h"
    #include "nbayes.h"
    #include "svmclass.h"
    #include "svm.h"
    #include "sockhelp.h"
    #include "strtok_r.h"
    extern int num_features_guess;
    void StopServerFun( int Signal );
%}
extern int num_features_guess;

class Case {
public:
    Case();
    ~Case();
};

class Feature {
public:
    Feature();
    ~Feature();
};

我运行痛饮:

swig -c++ -perl5 my_module.i

这会生成文件 my_module_wrap.cxx。 我编译:

g++ -c `perl -MConfig -e 'print join(" ", @Config{qw(ccflags optimize cccdlflags)}, "-I$Config{archlib}/CORE")'` my_module.cxx my_module_wrap.cxx

随后:

g++ `perl -MConfig -e 'print $Config{lddlflags}'` my_module.o my_module_wrap.o -o my_module.so

正如预期的那样,这确实创建了文件 my_module.so

那我试试这样用:

$ perl
use my_module;

这会导致以下错误:

Can't load './my_module.so' for module my_module: ./my_module.so: wrong ELF class: ELFCLASS64 at /usr/net/ActivePerl-5.14.2.1402/lib/DynaLoader.pm line 191.

从报错信息可以看出,我使用的是ActivePerl v5.14。 根据文档,Swig 应该支持 Perl 版本 >=5.8。否则,我看不出哪里可以更深入地挖掘。

有一个 similar question about Python,但这是使用不同 Python 解释器的用户错误。

Can't load './my_module.so' for module my_module: ./my_module.so: wrong ELF class: ELFCLASS64 

这意味着:您正在尝试将 64 位 my_module.so 加载到 32 位 perl 进程中。

您必须使用 64 位 perl,或者将 my_module.so 重建为 32 位共享库(通过添加 -m32 编译和 link 命令) .