[英]Create C++ interface for perl using SWIG?
I'm trying to grasp SWIG. 我正在尝试掌握SWIG。 Right now, I'm failing at building the simple module for perl. 现在,我无法为perl构建简单的模块。 Here's what I did: 这是我所做的:
example.cpp: example.cpp:
#include <iostream>
void hello()
{
std::cout << "Hello, world!"<< std::endl;
}
example.i: example.i:
%module example
%{
#include <iostream>
extern void hello();
%}
extern void hello();
app.pl: app.pl:
#!/usr/bin/perl
use strict;
use warnings;
use example;
example::hello();
and my compile.sh
script: 和我的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
Compiles fine - no errors are shown. 编译正常-没有错误显示。 However, when I run app.pl
, I get following error: 但是,当我运行app.pl
,出现以下错误:
/usr/bin/perl: symbol lookup error: ./example.so: undefined symbol: _Z5hellov
I tried to google a bit, but to no avail. 我尝试了一下谷歌,但无济于事。 So far, everything has failed. 到目前为止,一切都失败了。 What am I doing wrong? 我究竟做错了什么?
I'm using Linux Ubuntu x86_64 with 3.13 kernel, and perl 5.18.2 我正在使用带有3.13内核和perl 5.18.2的Linux Ubuntu x86_64
Behold the power of the small break. 看一下小突破的力量。
The reason is dead simple: I didn't add example.o
to the compile chain - so there indeed was no definition of hello() function! 原因很简单:我没有在编译链中添加example.o
因此确实没有hello()函数的定义!
To solve it, I changed compile.sh last line to this: g++ -shared example.o example_wrap.o -o example.so
为了解决这个问题,我将compile.sh的最后一行更改为: g++ -shared example.o example_wrap.o -o example.so
I feel stupid. 我觉得我好笨。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.