有谁知道怎么从perl里调用一个c库里的函数?????????

milson 2001-07-25 05:55:36
我看了www.perl.com上的相关文章,上面有一个例子是怎么用库里的函数,可是他的是把建立.pm文件和建立库放到了一起。我没有看明白怎么从perl里调用一个已经存在的库里的函数。
...全文
137 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
isaxu 2001-07-31
  • 打赏
  • 举报
回复
好惨!那你这100算是打水瓢了
去去 流水无情 ,有不是你解决的,为啥给你?
要是你贴点有用的东西才有可能阿
zeyong 2001-07-30
  • 打赏
  • 举报
回复
给我好了
milson 2001-07-30
  • 打赏
  • 举报
回复
To :isaxu(洒洒水) 
不能给自己啊。的确是解决了。
isaxu 2001-07-29
  • 打赏
  • 举报
回复
to milson
你是不是可以考虑把分数给自己,这样,我们也好知道你的问题解决了,好不好?
milson 2001-07-27
  • 打赏
  • 举报
回复
怎样从perl中调用c库里的函数

假如你所用的库是mylib.a 头文件是mylib.h
假如mylib.h像以下内容:
extern void hello();
hello()函数假如执行如下功能:
void hello()
{
printf("Success call from perl to c libray!\n";
}
一、建立工作目录mytest,把mylib.a和mylib.h放到mytest中
二、在mytest的上层目录执行
h2xs -O -n mytest ./mytest/mylib.h
三、进入mytest,修改mytest.xs,在文件末尾增加perl接口

void
hello()
CODE:
hello();

改变#include <./mytest/mylib.h>
为 #include "mylib.h"
一般情况下此行都需要修改。

四、修改Makefile.PL,其中#add begin和#add end中间的内容是新增加的。
use ExtUtils::MakeMaker;
# See lib/ExtUtils/MakeMaker.pm for details of how to influence
# the contents of the Makefile that is written.
WriteMakefile(
'NAME' => 'mytest',
'VERSION_FROM' => 'mytest.pm', # finds $VERSION
'LIBS' => [''], # e.g., '-lm'
'DEFINE' => '', # e.g., '-DHAVE_SOMETHING'
'INC' => '', # e.g., '-I/usr/include/other'
#add begin
'MYEXTLIB' => 'mylib.a',
#add end
);
#add begin
sub MY::postamble()
{
'
$(MYEXTLIB): .
';
#add end
}

五、执行perl Makefile.PL,让perl生成makefile
六、执行make
七、修改测试文件test.pl,修改完毕后改变为可执行
#!/usr/bin/perl
# Before `make install' is performed this script should be runnable with
# `make test'. After `make install' it should work as `perl test.pl'

######################### We start with some black magic to print on failure.

# Change 1..1 below to 1..last_test_to_print .
# (It may become useful if the test is moved to ./t subdirectory.)

use ExtUtils::testlib;
BEGIN { $| = 1; print "1..1\n"; }
END {print "not ok 1\n" unless $loaded;}
use mytest;
$loaded = 1;
print "ok 1\n";

######################### End of black magic.

# Insert your test code below (better if it prints "ok 13"
# (correspondingly "not ok 13") depending on the success of chunk 13
# of the test code):
mytest::hello();

八、此时你应该能够看到
Success call from perl to c libray!

呵呵,搞定!
milson 2001-07-26
  • 打赏
  • 举报
回复
谢谢,谢谢,我只是感觉这里人气太差,而且提出的问题,能解决得太少。
以我的能力做斑竹显然太差,而且我也不精通这个东东,我是赶鸭子上架啊。

还好我的问题解决了。

谢天谢地,谢谢各位。
netken 2001-07-25
  • 打赏
  • 举报
回复
那需要用 h2xs,xsubpp. 或 SWIG 来粘和啊
需要 C::Scan,DATA::Flow 两个模块,xs需要,会转化成一个xs中间语言
简单的方法是 h2xs -arg module_name xxxxx.h
然后安装他产生的模块,perl makefile.pl,make,make install
详细的麻烦的很,可以找相关资料看看
不是直接就能在perl 中 use,require xxx.h 的,hoho

还有就是,perl的斑竹需要的不是什么专项的知识,需要的是一中能创造氛围的能力
能让这个版“活”起来的力量。不然我想做斑竹就太容易了,而且这里很多人都早就可以做了。

其实做与c的粘和,除非在精确数据结构,复杂,需高效的数据处理的话,不太需要吧?
何必那么麻烦呢?
不过说起来,Socket.一些DB 等modules都用了xs

addoil
hoho
milson 2001-07-25
  • 打赏
  • 举报
回复
没有人弄过这样的程序吗?
等我把这个问题搞定,我要申请做perl的斑竹。
哈哈,我疯了。

2,204

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 CGI
社区管理员
  • CGI社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧