通过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\n");

保存退出

 

编译并安装,注意,安装需要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

相关文章:

  • 2021-11-26
  • 2021-08-08
  • 2022-02-09
  • 2021-10-05
  • 2021-09-05
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案