第二讲
perl 加载c/c++的库

先通过h2xs 创建一个新的工程

h2xs -A -n two_test

进入目录

cd two_test

创建一个mylib文件夹,存放静态库

mkdir mylib

c程序头文件chen.h

#include<stdio.h>
void chen(char *);

c程序代码chen.c

#include "chen.h"
void chen(char * name){
printf("input string is : %s\n", name);
}

编译静态库

gcc -c chen.c
ar -r libchen.a chen.o

将libchen.a静态库与chen.h头文件拷贝到two_test/mylib

cp libchen.a two_test/mylib/
cp chen.h two_test/mylib

修改perl 生成makefile 的脚本Makefile.PL

use 5.014002;
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
NAME => 'two_test',
VERSION_FROM => 'lib/two_test.pm', # finds $VERSION
PREREQ_PM => {}, # e.g., Module::Name => 1.1 ($] >= 5.005 ? ## Add these new keywords supported since 5.005
(ABSTRACT_FROM => 'lib/two_test.pm', # retrieve abstract from module
AUTHOR => 'root <root@>') : ()),
LIBS => [''], # e.g., '-lm'
DEFINE => '', # e.g., '-DHAVE_SOMETHING'
INC => '-I.', # e.g., '-I. -I/usr/include/other'
# Un-comment this if you add C files to link with later:
# OBJECT => '$(O_FILES)', # link all the C files too
MYEXTLIB => 'mylib/libchen.a'
);

MYEXTLIB => 'mylib/libchen.a' 是新增的命令,作用是在生成perl 扩展包时包含libchen.a的库

修改two_test.xs 文件

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h" #include "ppport.h"
#include "mylib/chen.h" MODULE = two_test PACKAGE = two_test void
chen(name)
char * name

红色部分为增加代码

编译并安装,安装需要root 权限

perl Makefile.PL
make
make install

编写一个测试程序two_test.pl, 测试扩展

#!/usr/bin/perl
use two_test;
$one = "sfjak";
&two_test::chen($one);

执行程序

perl two_test.pl

输出:
input string is : sfjak

成功调用扩展

参考文章:
一个简单例子
http://www.chinaunix.net/old_jh/25/951221.html
IBM介绍如何使用c扩展perl
http://www.ibm.com/developerworks/cn/aix/library/0908_tangming_perltoc/
介绍如何使用C++扩展perl

最新文章

  1. TextView中的部分文字响应点击事件
  2. leetCode 354. Russian Doll Envelopes
  3. 【Oracle】如何导库
  4. hibernate执行session.createQuery(hql)时hql若有参数则报错
  5. BZOJ 1600
  6. 【leetcode❤python】 Maximum Depth of Binary Tree
  7. $( document ).ready()&amp;$(window).load()
  8. Xdebug+phpstorm配置
  9. Java内存模型浅析
  10. Android学习笔记:利用httpclient和AsyncTask 发起网络http post操作
  11. leetcode第九题--Palindrome Number
  12. Aspose.Words提示The document appears to be corrupted and cannot be loaded.
  13. tensorflow的特征工程函数
  14. JavaScript知识精简
  15. 图解HTTP第一章
  16. 绘图QPainter-字体
  17. [转]linux(ubuntu)上运行网易popo
  18. centos6安装多实例mysql
  19. BTrace学习总结
  20. 【题解】Luogu P2047 社交网络总结 (Floyd算法,最短路计数)

热门文章

  1. The JSP specification requires that an attribute name is preceded by whitespace--异常
  2. Struts多个文件上传
  3. XMPP学习笔记 -- RFC 6120
  4. html5学习笔记(1)-新标签
  5. mysql优化20条原则
  6. LA-5059(组合游戏)
  7. July Cook-Off 2017
  8. [YNOI 2016] 掉进兔子洞
  9. CentOS7设置系统/yum以及firefox web代理上网
  10. Strom配置说明