NativeCall 加载我不调用的库符号 [英] NativeCall loading a library symbol I don't call

查看:36
本文介绍了NativeCall 加载我不调用的库符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个库,我想调用第一个库中的例程,然后它们调用第二个库中的例程,但由于这些符号未定义而崩溃.即使我不想调用它们,是否也可以从库 XX 中说加载这些符号"?

I have two libraries, I want to call routines in the first library, they then call routines in the second library, but crash because those symbols are undefined. Is it possible to say "load these symbols" from library XX even though I don't want to call them?

testlib1.c:

testlib1.c:

#include <stdio.h>
void sub2();
void sub1() {
  printf("Called sub1\n");
  sub2();
}

testlib2.c:

testlib2.c:

#include <stdio.h>
void sub2() {
  printf("Called sub2\n");
}

testit.p6:

use NativeCall;

sub sub1() is native('testlib1') {}
sub sub2() is native('testlib2') {}

sub1();

错误:

Cannot locate native library 'libtestlib1.so': ./libtestlib1.so: undefined symbol: sub2

如果我在调用 sub1 之前手动调用 sub2,它工作正常,但我不想这样做..

If I call sub2 manually before calling sub1, it works fine, but I don't want to do that..

推荐答案

好的,这使它可以工作,但它是一种不可移植的解决方法——它仅适用于使用 dyncall 构建的 MoarVM.似乎应该有一些公开的函数到 NativeCall 世界可以移植.

Ok, This makes it work, but is a non-portable workaround -- it only works if your MoarVM is built with dyncall. It seems like there should be some exposed function to NativeCall world that does this portably.

use NativeCall;                                                                 

sub dlLoadLibrary(Str --> Pointer) is native {}                                 
dlLoadLibrary('libtestlib2.so');                                                

sub sub1() is native('testlib1') {}                                             

sub1();                                                                         

dlLoadLibrary 是加载动态库的 dyncall 方式,这显然足以解析符号.

dlLoadLibrary is the dyncall way to load a dynamic library, which is apparently enough for the symbol to be resolved.

来自@jnthn 的更好建议:

Better suggestion from @jnthn:

sub fake() is native('testlib2');
try fake();

fake() 加载 testlib2,抛出一个 Exception 因为 fake 不是一个真正的例程库,但 try 忽略 Exception.

fake() loads testlib2, throws an Exception because fake isn't a real routine in that library, but try ignores the Exception.

这篇关于NativeCall 加载我不调用的库符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆