C++ 使用内联CPP调用so文件中的函数不起作用
我有.h和.cpp文件: 说你好,h:C++ 使用内联CPP调用so文件中的函数不起作用,c++,perl,shared-libraries,C++,Perl,Shared Libraries,我有.h和.cpp文件: 说你好,h: extern "C" { void say_hi(); } 说_hi.cpp: #include <cstdio> #include "say_hi.h" void say_hi(){ printf("hello workd!\n"); } 然后使用perl内联CPP调用它: >perl -e 'use Inline CPP=>q{ \ #include "say_hi.
extern "C" {
void say_hi();
}
说_hi.cpp:
#include <cstdio>
#include "say_hi.h"
void say_hi(){
printf("hello workd!\n");
}
然后使用perl内联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文件
test.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\u to/libsay\u hi.解决问题。因此“
而不是LIBS=>”-l.“
更多详细信息检查void say\u hi()
实现没有进入extern“C”
。感谢Jarod42的回复,在现实生活中,say\u hi()函数可以是任何风格的代码,已经编译成了。因此,我的理解只是在.h文件中,它需要声明为extern“C”,这就是为什么我从test.cpp进行的纯调用有效,问题不在于.so,而是在perl内联cpp中,我认为。通过使用MYEXTLIB=>'/full/path_to/libsay_hi.so'
而不是LIBS=>“-l…”解决了问题
More details check@codesoar您应该将此添加为回答而不是评论。您应该将更多详细信息放在此处。
>test
hello workd!