作者gigigigi (gigigigi)
看板C_and_CPP
标题[问题]c++ virtual 问题
时间Fri Jul 27 20:02:39 2018
开发平台(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
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 175.181.225.221
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1532692962.A.FC4.html
※ 编辑: gigigigi (175.181.225.221), 07/27/2018 20:23:45
1F:推 Sex5F: prototype? 07/28 01:55
2F:推 cole945: 板上搜 [问题] dynamic shared library设计问题 07/28 11:22
3F:→ cole945: 会有一些你想要的答案 07/28 11:22
4F:→ cole945: non-virtual 的话是直接呼叫 triangle::getaa() 07/28 11:23
5F:→ cole945: virtual 的话, 会透过 vtable (function pointer) 不会 07/28 11:23
6F:→ cole945: 直接呼叫 getaa 07/28 11:23