网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > linux > Linux编程 > Perl编程 > 怎样从Perl中调用C库里的函数  

怎样从Perl中调用C库里的函数

2004-09-17  作者:bitsCN整理  来源:CSDN.NET  点评 投稿 收藏


  假如你所用的库是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

网管u家u.bitscn@com


    '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"; }
网管bitscn_com

    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!

TAGs   函数   调用   怎样   mytest   to   perl   #add   of   hello   执行   修改      
 上一篇:用Perl语言进行Socket网络编程   下一篇:使用Perl连接Access数据库详解
怎样从Perl中调用C库里的函数 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: