Perl

本类阅读TOP10

·在Perl中使用sendmail发送MIME邮件
·Cultured Perl: 吸引 C 和 Java 程序员目光的 Perl 5.6
·传奇程序员Larry Wall:Perl的乐趣
·深入研究表单提交方式:GET/POST
·Bioperl的简介
·吸引WEB程序员目光的Mason
·perl如何内嵌html。
·中英文混合字符截取
·从一个安装文件看CGI的安全性
·PERL连接ACCESS数据库

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
怎样从perl中调用c库里的函数

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

怎样从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!

    呵呵,搞定!




相关文章

相关软件