免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1904 | 回复: 0
打印 上一主题 下一主题

perl如何调用其它编程语言(如C) [复制链接]

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-07-03 16:54 |只看该作者 |倒序浏览
问题描述:
unix环境下,在perl中
1、如何调用一个标准C函数。
2、如何调用一个4gl编写的函数。
3、如何调用一个操作系统函数或系统调用。

操作系统环境:
linux

具体测试代码和例子:


Makefile.PL内容如下:
代码:

  1. use ExtUtils::MakeMaker;
  2. # See lib/ExtUtils/MakeMaker.pm for details of how to influence
  3. # the contents of the Makefile that is written.
  4. WriteMakefile(
  5.     'NAME'              =>; 'Mytest',
  6.     'VERSION_FROM'      =>; 'Mytest.pm', # finds $VERSION
  7.     'PREREQ_PM'         =>; {}, # e.g., Module::Name =>; 1.1
  8.     'LIBS'              =>; [''], # e.g., '-lm'
  9.     'DEFINE'            =>; '', # e.g., '-DHAVE_SOMETHING'
  10.     'INC'               =>; '', # e.g., '-I/usr/include/other'
  11. );
复制代码




Mytest.PM的内容如下:
代码:

  1. package Mytest;

  2. require 5.005_62;
  3. use strict;
  4. use warnings;

  5. require Exporter;
  6. require DynaLoader;

  7. our @ISA = qw(Exporter DynaLoader);

  8. # Items to export into callers namespace by default. Note: do not export
  9. # names by default without a very good reason. Use EXPORT_OK instead.
  10. # Do not simply export all your public functions/methods/constants.

  11. # This allows declaration       use Mytest ':all';
  12. # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
  13. # will save memory.
  14. our %EXPORT_TAGS = ( 'all' =>; [ qw(

  15. ) ] );

  16. our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
  17. our @EXPORT = qw(

  18. );
  19. our $VERSION = '0.01';

  20. bootstrap Mytest $VERSION;

  21. # Preloaded methods go here.

  22. 1;
  23. __END__
  24. # Below is stub documentation for your module. You better edit it!

  25. =head1 NAME

  26. Mytest - Perl extension for blah blah blah

  27. =head1 SYNOPSIS

  28.   use Mytest;
  29.   blah blah blah
  30. =head1 DESCRIPTION

  31. Stub documentation for Mytest, created by h2xs. It looks like the
  32. author of the extension was negligent enough to leave the stub
  33. unedited.

  34. Blah blah blah.

  35. =head2 EXPORT

  36. None by default.


  37. =head1 AUTHOR

  38. A. U. Thor, a.u.thor@a.galaxy.far.far.away

  39. =head1 SEE ALSO

  40. perl(1).

  41. =cut
复制代码




Mytest.xs内容如下:

代码:

  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"

  4. MODULE = Mytest         PACKAGE = Mytest

复制代码


上面这个是大体的框架,我现在自己写了一个c函数,例如:
代码:

  1. #include <stdio.h>;

  2. static int testfun( int );

  3. int main( void )
  4. {
  5.     (void)fprintf(stderr,"Sample for test.\n");
  6.     if( testfun( 100 ) < 0 ) {
  7.         (void)fprintf(stderr,"testfun error!\n");
  8.         exit(-1);
  9.     }
  10.     exit(0);
  11. }

  12. int testfun(int num )
  13. {
  14.     if( num >; 0 )
  15.         printf("Int num = %d\n", num );
  16.     else
  17.         return -1;
  18.     return 0;
  19. }

复制代码


需求:
现在如何在上面那几个文件中添加,让perl可以执行c的函数。
如果使用其它的方式,怎么使用,具体的例子是什么
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP