[問題]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