perl C/C++ 扩展(一)

通过h2xs 中间件,我们可以快速的使用c或则C++ 库来实现perl 扩展功能

第一讲:
跑通hello world 程序
******************************
我们使用命令:
h2xs -A -n test
它会帮助你建立一个test的文件夹,里面已经初始化部分文件了

进入test目录

cd test

打开文件test.xs

vi test.xs

原文件内容为:

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"


MODULE = test PACKAGE = test

我们增加一个函数变成

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"


MODULE = test PACKAGE = test

void
hello_world(char *classname)
CODE:
printf("hello world from perl xs language
");

保存退出

编译并安装,注意,安装需要root权限
perl Makefile.PL
make && make install

编写一个测试的perl 程序,test.pl

#!/usr/bin/perl
use test;
test->hello_world();

保存退出

执行测试程序
perl test.pl

没出什么意外的话,输出
hello world from perl xs language

原文地址:https://www.cnblogs.com/chenfool/p/3897033.html