SWIG:仅使用头和共享库包装Perl的C ++,无法找到可加载的对象错误 [英] SWIG: Wrapping C++ for Perl using only a header and a shared library, can't locate loadable object error

查看:95
本文介绍了SWIG:仅使用头和共享库包装Perl的C ++,无法找到可加载的对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习SWIG,并且遇到一些问题,让SWIG能够在Linux机器上使用perl。我有Dog.h,Crow.h,Animal.i和libmylib.so文件。所有这些文件都在同一个目录中。 libmylib.so是使用Dog.cpp和Crow.cpp编译的,分别引用Dog.h和Crow.h。 My Animal.i文件如下:

 %module动物
%{
/ *包含包装代码中的头文件* /
#includeDog.h
#includeCrow.h
%}

/ *解析头文件到生成包装* /
%includeDog.h
%includeCrow.h

下面是我为了构建perl模块而执行的命令:

  swig -perl  - c ++ Animal.i 
g ++ -shared -fPIC Animal_wrap.cxx -L。 -lmylib -I / usr / lib64 / perl5 / CORE -o _Animal.so
LD_LIBRARY_PATH =。 perl

当我输入use Animal;时,出现以下错误:Can not locate在@INC中为模块Animal加载对象。我是相当新的Perl,所以我不知道如何去解决这个问题,虽然从四处搜寻我觉得问题可能是Perl无法引用我的libmylib.so文件。任何帮助将不胜感激。谢谢!

解决方案

以下内容似乎适用于Ubuntu 16.04:

< h2>档案:

Animal.i:

 %module Animal 
%{
#includeDog.h
#includeCrow.h
%}
%includeDog。 h
%包括Crow.h

Crow.h < (b

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'
}
virtual〜Crow(){
ncrows--;
}
static int ncrows;
};

Dog.h:

  class Dog {
public:
Dog(){
ndogs ++;
}
virtual〜Dog(){
ndogs--;
}
static int ndogs;
};

Crow.cpp:

  #includeCrow.h
int Crow :: ncrows = 0;

Dog.cpp:

  #includeDog.h
int Dog :: ndogs = 0;

test.pl:

  use strict; 
使用警告;
使用Animal;

printCreating a Crow:\\\
;
my $ c = Animal :: Crow-> new();
printCreated crow $ c \\\
;
$ c-> DESTROY();
打印创建一只狗:\\\
;
my $ d = Animal :: Dog-> new();
打印Created dog $ d \\\
;
$ d-> DESTROY();



汇编:



  swig -perl -c ++ Animal.i 
g ++ -fPIC -c Crow.cpp
g ++ -fPIC -c Dog.cpp
g ++ -shared Crow.o Dog.o -o libmylib.so
g ++ -fPIC -c Animal_wrap.cxx -I / usr / lib / x86_64-linux-gnu / perl / 5.22 / CORE
g ++ -shared -L。 Animal_wrap.o -lmylib -o Animal.so



运行测试脚本:



  $ LD_LIBRARY_PATH =。 perl test.pl 
创建乌鸦:
创建乌鸦动物::乌鸦= HASH(0x10c2eb0)
创建狗:
创建狗动物::狗= HASH(0x10c2f88)


I'm trying to learn SWIG and I'm having some issues getting SWIG to work with perl on a Linux machine. I have the files Dog.h, Crow.h, Animal.i, and libmylib.so. All these files are in the same directory. libmylib.so was compiled using Dog.cpp and Crow.cpp, which reference Dog.h and Crow.h respectively. My Animal.i file is as follows:

%module Animal
%{
/* Includes the header in the wrapper code */
#include "Dog.h"
#include "Crow.h"
%}

/*Parse the header file to generate wrappers */
%include "Dog.h"
%include "Crow.h"

Here are the commands that I'm executing in order to build the perl module:

swig -perl -c++ Animal.i
g++ -shared -fPIC Animal_wrap.cxx -L. -lmylib -I/usr/lib64/perl5/CORE -o _Animal.so
LD_LIBRARY_PATH=. perl

When I type "use Animal;", I get the following error: "Can't locate loadable object for module Animal in @INC". I'm fairly new to perl so I'm not sure how to go about fixing the issue, although from searching around I feel like the problem might be that perl cannot reference my libmylib.so file. Any help would be greatly appreciated. Thank you!

解决方案

The following seems to work on Ubuntu 16.04:

Files:

Animal.i:

%module Animal
%{
#include "Dog.h"
#include "Crow.h"
%}
%include "Dog.h"
%include "Crow.h"

Crow.h

class Crow {
public:
    Crow()  {
        ncrows++;
    }
    virtual ~Crow() {
        ncrows--;
    }
    static  int ncrows;
};

Dog.h:

class Dog {
public:
    Dog()  {
        ndogs++;
    }
    virtual ~Dog() {
        ndogs--;
    }
    static  int ndogs;
};

Crow.cpp:

#include "Crow.h"
int Crow::ncrows = 0;

Dog.cpp:

#include "Dog.h"
int Dog::ndogs = 0;

test.pl:

use strict;
use warnings;
use Animal;

print "Creating a Crow:\n";
my $c = Animal::Crow->new();
print "    Created crow $c\n";
$c->DESTROY();
print "Creating a Dog:\n";
my $d = Animal::Dog->new();
print "    Created dog $d\n";
$d->DESTROY();

Compilation:

swig -perl -c++ Animal.i
g++ -fPIC -c Crow.cpp
g++ -fPIC -c Dog.cpp
g++ -shared Crow.o Dog.o -o libmylib.so
g++ -fPIC -c Animal_wrap.cxx -I/usr/lib/x86_64-linux-gnu/perl/5.22/CORE
g++ -shared -L. Animal_wrap.o -lmylib -o Animal.so

Running test script:

$ LD_LIBRARY_PATH=. perl test.pl 
Creating a Crow:
    Created crow Animal::Crow=HASH(0x10c2eb0)
Creating a Dog:
    Created dog Animal::Dog=HASH(0x10c2f88)

这篇关于SWIG:仅使用头和共享库包装Perl的C ++,无法找到可加载的对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆