Exporting C ++ Class Symbols Using GCC

I use dlopen to combine the characters 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 compile this with g++ -Wl,--export-dynamic -o test test.cppand checking for characters with nm -g test. I expect that symbols will exist in the executable _ZN3FooC1Evand _ZN3FooC2Ev, since they need a dynamic library, but they will not be displayed if I do not use them without commenting on the above line. I believe that it is optimized because GCC believes that it is not needed.

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

+4
source share
2 answers

, , , . , , ...

+3

, :

$ gcc -shared -fPIC -o lib1.so lib1.c

-fPIC GCC .
-

test.so, :

$ gcc -L$(pwd) -Wl,-rpath=$(pwd) --enable-new-dtags -o test main.c -l1 -l2

-rpath =/my/location
, : $ readelf -d test

0

Source: https://habr.com/ru/post/1655444/


All Articles