perl如何调用其它编程语言(如C)
问题描述:
unix环境下,在perl中
1、如何调用一个标准C函数。
2、如何调用一个4gl编写的函数。
3、如何调用一个操作系统函数或系统调用。
操作系统环境:
linux
具体测试代码和例子:
Makefile.PL内容如下:
代码:
- 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
- 'PREREQ_PM' =>; {}, # e.g., Module::Name =>; 1.1
- 'LIBS' =>; [''], # e.g., '-lm'
- 'DEFINE' =>; '', # e.g., '-DHAVE_SOMETHING'
- 'INC' =>; '', # e.g., '-I/usr/include/other'
- );
复制代码
Mytest.PM的内容如下:
代码:
- package Mytest;
- require 5.005_62;
- use strict;
- use warnings;
- require Exporter;
- require DynaLoader;
- our @ISA = qw(Exporter DynaLoader);
- # Items to export into callers namespace by default. Note: do not export
- # names by default without a very good reason. Use EXPORT_OK instead.
- # Do not simply export all your public functions/methods/constants.
- # This allows declaration use Mytest ':all';
- # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
- # will save memory.
- our %EXPORT_TAGS = ( 'all' =>; [ qw(
- ) ] );
- our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
- our @EXPORT = qw(
- );
- our $VERSION = '0.01';
- bootstrap Mytest $VERSION;
- # Preloaded methods go here.
- 1;
- __END__
- # Below is stub documentation for your module. You better edit it!
- =head1 NAME
- Mytest - Perl extension for blah blah blah
- =head1 SYNOPSIS
- use Mytest;
- blah blah blah
- =head1 DESCRIPTION
- Stub documentation for Mytest, created by h2xs. It looks like the
- author of the extension was negligent enough to leave the stub
- unedited.
- Blah blah blah.
- =head2 EXPORT
- None by default.
- =head1 AUTHOR
- A. U. Thor, a.u.thor@a.galaxy.far.far.away
- =head1 SEE ALSO
- perl(1).
- =cut
复制代码
Mytest.xs内容如下:
代码:
- #include "EXTERN.h"
- #include "perl.h"
- #include "XSUB.h"
- MODULE = Mytest PACKAGE = Mytest
复制代码
上面这个是大体的框架,我现在自己写了一个c函数,例如:
代码:
- #include <stdio.h>;
- static int testfun( int );
- int main( void )
- {
- (void)fprintf(stderr,"Sample for test.\n");
- if( testfun( 100 ) < 0 ) {
- (void)fprintf(stderr,"testfun error!\n");
- exit(-1);
- }
- exit(0);
- }
- int testfun(int num )
- {
- if( num >; 0 )
- printf("Int num = %d\n", num );
- else
- return -1;
- return 0;
- }
复制代码
需求:
现在如何在上面那几个文件中添加,让perl可以执行c的函数。
如果使用其它的方式,怎么使用,具体的例子是什么
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论