Last active
November 20, 2020 02:55
-
-
Save shininglion/0520716075053d5e508f85f0b1f151b9 to your computer and use it in GitHub Desktop.
when header and related shared lib mismatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <dlfcn.h> | |
| #include "testlib_old.h" | |
| typedef A* (*create_obj_t)(); | |
| typedef void (*free_obj_t)(A* obj); | |
| int main() | |
| { | |
| void* lib = dlopen("./libtestlib.so", RTLD_GLOBAL | RTLD_NOW); | |
| create_obj_t create_obj_ptr = (create_obj_t)dlsym(lib, "create_obj"); | |
| free_obj_t free_obj_ptr = (free_obj_t)dlsym(lib, "free_obj"); | |
| A* obj = create_obj_ptr(); | |
| obj->print(); // It will call B::hidden | |
| free_obj_ptr(obj); | |
| dlclose(lib); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CC = g++ -O3 | |
| EXE = run | |
| SO = libtestlib.so | |
| all: $(SO) $(EXE) | |
| $(EXE): main.cpp testlib.h | |
| $(CC) -o $@ $< -ldl | |
| $(SO): testlib.cpp testlib.h | |
| $(CC) -fPIC -c $< | |
| $(CC) -shared *.o -o $(SO) | |
| clean: | |
| rm -f $(EXE) $(SO) *.o | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include "testlib_new.h" | |
| A* create_obj() | |
| { | |
| return new B(); | |
| } | |
| void free_obj(A* obj) | |
| { | |
| delete obj; | |
| } | |
| void A::print() | |
| { | |
| puts("print function in A"); | |
| } | |
| void A::hidden() | |
| { | |
| puts("hidden function in A"); | |
| } | |
| void B::print() | |
| { | |
| puts("print function in B"); | |
| } | |
| void B::hidden() | |
| { | |
| puts("hidden function in B"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef TESTLIB_H | |
| #define TESTLIB_H | |
| class A | |
| { | |
| public: | |
| virtual ~A() = default; | |
| virtual void hidden(); | |
| virtual void print(); | |
| }; | |
| class B : public A | |
| { | |
| public: | |
| void hidden() override; | |
| void print() override; | |
| }; | |
| extern "C" { | |
| A* create_obj(); | |
| void free_obj(A* obj); | |
| } | |
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef TESTLIB_H | |
| #define TESTLIB_H | |
| class A | |
| { | |
| public: | |
| virtual ~A() = default; | |
| virtual void print(); | |
| }; | |
| class B : public A | |
| { | |
| public: | |
| void print() override; | |
| }; | |
| extern "C" { | |
| A* create_obj(); | |
| void free_obj(A* obj); | |
| } | |
| #endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment