3
我有h和.cpp文件:使用内联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内联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
但是,如果我考.so文件下面.cpp文件 TEST.CPP:
#include "say_hi.h"
main() {
say_hi();
}
编译:
g++ -L. -lsay_hi test.cpp -o test
运行测试:
>test
hello workd!
我的Perl(v5.8.0)有什么问题,或者我错过了什么吗?
非常感谢!
你'无效say_hi()解决'执行不进'的extern “C”'... – Jarod42
感谢Jarod42您的回复,在现实生活中say_hi ()funcion可以是任何代码风格,已经编译成.so,我的理解只是在.h文件中,它需要声明为extern“C”,这就是为什么我的纯调用test.cpp工作,问题是不与.so,但在perl内联CPP我认为。 – codesoar
问题通过使用 解决'MYEXTLIB =>'/ full/path_to/libsay_hi.so'' 而不是 'LIBS =>“ - l ...”' 更多详情请查看[这里](http:// www .perlmonks.org /?node_id = 1069902) – codesoar