Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export symbols of C++ class with GCC

Tags:

c++

gcc

I am using dlopen to merge symbols of a dynamically loaded library and its host, and in the host I have a class:

class Foo {
public:
    Foo() {/* ... */}
    void bar() {/* ... */}
};

int main() {
    // Foo foo;
    return 0;
}

I am compiling this with g++ -Wl,--export-dynamic -o test test.cpp and inspecting the symbols with nm -g test. I expect the symbols _ZN3FooC1Ev and _ZN3FooC2Ev to exist in the executable, since the dynamic library needs them, but they do not appear unless I use them by un-commenting the above line. I believe it is being optimized out, since GCC thinks it is not needed.

How do I force the constructor and methods of Foo to be included in the host binary?

like image 421
Vortico Avatar asked Oct 18 '22 01:10

Vortico


1 Answers

A class definition with inlined method definitions do not generate any symbol if you don't, at least, define an object. So don't define the methods as inlined if you want to ensure their visibility...

like image 171
Jean-Baptiste Yunès Avatar answered Nov 13 '22 19:11

Jean-Baptiste Yunès