这实际上相当简单:
你好.h
#ifndef H_HELLO
const char *hello(void);
#define H_HELLO
#endif
你好ç
const char *
hello(void) {
return "Hello";
}
例子.xs
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
#include "hello.h"
MODULE = My::Example PACKAGE = My::Example PREFIX = MY_
PROTOTYPES: DISABLE
const char *
MY_hello()
CODE:
RETVAL = hello();
OUTPUT:
RETVAL
t/My-Example.t
use strict;
use warnings;
use Test::More;
BEGIN { use_ok('My::Example') };
is(My::Example::hello(), 'Hello', 'hello returns "Hello"');
done_testing;
[~/tmp/My-Example]$ 证明 -vb t/My-Example.t
t/My-Example.t ..
好的 1 - 使用 My::Example;
好的 2 - 你好返回“你好”
1..2
好的
所有测试成功。
文件=1,测试=2,0 挂钟秒
(0.04 usr 0.01 sys + 0.03 cusr 0.00 csys = 0.08 CPU)
结果:通过
由Makefile.PL
生成h2xs
。我唯一改变的是取消注释以下行:
# 如果您添加 C 文件以供稍后链接,请取消注释:
OBJECT => '$(O_FILES)', # 也链接所有 C 文件