how to call function in executable from my library?

前端 未结 2 888
悲哀的现实
悲哀的现实 2020-11-28 15:29

I have an executable and a dynamic library (.so). The library exports some symbols and executable calls it successfully. But I want to make possible to library call executab

相关标签:
2条回答
  • 2020-11-28 15:34

    An easier way to achieve this is to have the executable register a function for later use by the library, the library stores a pointer to the function, and may call it at a later time.

    0 讨论(0)
  • 2020-11-28 15:40

    In Linux/ELF you can pass the -export-dynamic option to the linker (-rdynamic on the compiler driver gcc) so symbols from the executable are exported to shared objects.

    You'd have a dummy print implementation in your library, which would be shadowed by the implementation on your executable, since the executable is usually searched before shared objects for symbol resolution.

    This has the disadvantage that it's not very fine-grained, you could end up overriding some symbol you didn't intend to. The finer-grained option would be to create a list of symbols to be exported as:

    {
        print;
        <other symbols>
    };
    

    and pass that list to the linker, e.g. from gcc: -Wl,--dynamic-list=<file with list of symbols>

    0 讨论(0)
提交回复
热议问题