使用SWIG为perl创建C ++接口?

时间:2015-10-30 11:17:05

标签: c++ linux perl swig

我试图抓住SWIG。现在,我没有为perl构建简单的模块。这就是我的所作所为:

example.cpp:

#include <iostream>

void hello()
{
    std::cout << "Hello, world!"<< std::endl;
}

example.i:

%module example
%{
#include <iostream>
extern void hello();
%}

extern void hello();

app.pl:

#!/usr/bin/perl

use strict;
use warnings;

use example;

example::hello();

和我的compile.sh脚本:

#!/bin/bash -ex

arch=`perl -e 'use Config; print $Config{archlib};'`    

swig3.0 -c++ -perl5 example.i
g++ -fPIC -c example.cpp
g++ -fPIC -c example_wrap.cxx -I$arch/CORE -L$arch/CORE
g++ -shared example_wrap.o -o example.so

编译正常 - 没有显示错误。但是,当我运行app.pl时,我收到以下错误:

/usr/bin/perl: symbol lookup error: ./example.so: undefined symbol: _Z5hellov

我试着谷歌一点,但无济于事。到目前为止,一切都失败了。我做错了什么?

我使用Linux Ubuntu x86_64和3.13内核,以及perl 5.18.2

1 个答案:

答案 0 :(得分:1)

看到小休息的力量。

原因很简单:我没有将example.o添加到编译链中 - 所以确实没有hello()函数的定义!

要解决这个问题,我将compile.sh最后一行更改为: g++ -shared example.o example_wrap.o -o example.so

我觉得自己很蠢。