[问题]c++ virtual 问题

楼主: gigigigi (gigigigi)   2018-07-27 20:02:39
开发平台(Platform): (Ex: Win10, Linux, ...)
Linux
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
g++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
N/A
问题(Question):
https://www.sendspace.com/file/7gq1j2 这是我程式码地方
我是在把一个 继承class 打包 so档案 之后透过 dlopen load 这个so
如果我把 triangle.hpp 里面的 virtual void getaa() const;
的virtual拿掉会编译阶段的link 错误无法编译过
但是我把这程式码单独写在同个档案, 不用so + dlopen 是正常可以编译过
不懂为什么 so 方式去dlopen link 阶段会错
错误讯息如下:
g++ -Wall -pedantic -ggdb3 -O0 -std=c++11 -shared -fPIC -o triangles.so
triangle.cpp
g++ -Wall -pedantic -ggdb3 -O0 -std=c++11 main.cpp -o main -ldl -lpthread
/tmp/ccB1oVpb.o: 于函式 main:
dlopen_test/main.cpp:42: 未定义参考到 triangle::getaa() const
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'a.out' failed
make: *** [a.out] Error 1
谢谢
作者: Sex5F (HTC)   2018-07-28 01:55:00
prototype?
作者: cole945 (跶跶..)   2018-07-28 11:22:00
板上搜 [问题] dynamic shared library设计问题会有一些你想要的答案non-virtual 的话是直接呼叫 triangle::getaa()virtual 的话, 会透过 vtable (function pointer) 不会直接呼叫 getaa

Links booklink

Contact Us: admin [ a t ] ucptt.com