gpt4 book ai didi

c++ - 使用内联 CPP 调用 so 文件中的函数不起作用

转载 作者:太空狗 更新时间:2023-10-29 23:17:00 26 4
gpt4 key购买 nike

我有 .h 和 .cpp 文件:

say_hi.h:

   extern "C" {
void say_hi();
}

say_hi.cpp:

   #include <cstdio>
#include "say_hi.h"
void say_hi(){
printf("hello workd!\n");
}

然后我编译这个

g++ -I. -fPIC -c *.cpp
g++ -shared *.o -I. -olibsay_hi.so

然后使用perl Inline CPP调用它:

>perl -e 'use Inline CPP=>q{  \
#include "say_hi.h" \
void test(){return say_hi(); } \
}, INC=>"-I.",LIBS=>"-lsay_hi"; test()'

我得到了:

perl: symbol lookup error: /home/xxx/test_so/_Inline/lib/auto/e_8555/e_8555.so: undefined symbol: say_hi

但是如果我用下面的 .cpp 文件测试 .so 文件测试.cpp:

#include "say_hi.h"
main() {
say_hi();
}

编译它:

g++ -L. -lsay_hi test.cpp -o test

运行测试:

>test
hello workd!

我的 Perl (v5.8.0) 有什么问题吗?或者我错过了什么?

非常感谢!

最佳答案

问题通过使用 MYEXTLIB => '/full/path_to/libsay_hi.so' 而不是 LIBS=>"-l..." 解决了更多细节检查 here

关于c++ - 使用内联 CPP 调用 so 文件中的函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20947417/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com