如何从Perl模块的Inline C块调用另一模块的Inline C函数
解决Perl Inline C跨模块调用函数的问题
核心修正与实现步骤
1. 修正函数声明错误
你当前的extern声明返回值类型写错了,原函数get_instrument_price返回的是int,不是int*,先把这个语法错误改掉:
extern int get_instrument_price(int type);
2. 加载依赖模块
在调用方的Dev::Instrument::Detail模块开头,必须先加载被调用的Dev::Instrument::Price模块,确保对应的共享库已被编译生成:
use Dev::Instrument::Price;
3. 配置正确的链接参数
Inline C会按包名路径结构存放编译后的共享库,Dev::Instrument::Price的库文件路径为_Inline/lib/auto/Dev/Instrument/Price/,库名一般是Price.so(Linux/macOS)或Price.dll(Windows)。在调用方的Inline C参数里指定链接路径和库名:
use Inline C => <<'__END_OF_C__', LIBS => '-L_Inline/lib/auto/Dev/Instrument/Price -lPrice';
4. 确保编译顺序
首次运行时,可先手动编译依赖模块生成共享库,避免路径问题:
perl -c Dev/Instrument/Price.pm
完整修正代码示例
被调用模块:Dev/Instrument/Price.pm
package Dev::Instrument::Price; use warnings 'all'; use strict; use Inline C => <<'__END_OF_C__'; int get_instrument_price(int type) { return 154; } __END_OF_C__ 1; # Perl模块必须返回true
调用模块:Dev/Instrument/Detail.pm
package Dev::Instrument::Detail; use warnings 'all'; use strict; use Dev::Instrument::Price; use Inline C => <<'__END_OF_C__', LIBS => '-L_Inline/lib/auto/Dev/Instrument/Price -lPrice'; extern int get_instrument_price(int type); int get_instrument_detail(int type) { // some code int instrument_price = get_instrument_price(type); // some more code return instrument_price; } __END_OF_C__ 1;
内容的提问来源于stack exchange,提问作者Lalit Mehra
相关产品推荐
相关产品推荐

