ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
It doesn't work. What you may try is to add the object file from the executable to the library. Or, if that solution does not suit you, you may add a new function to your library that will get a function pointer from the application. Then the pointer (pointing to the function in question, of course) may be used in the library. Or...maybe move the function that calls the function in question to the executable?
Linux defaults to interposing earlier definitions during loading, which probably gives you an easy solution. I don't know enough of the exact rules, so if I were doing it myself, I would need to experiment to see what works.
The basic idea would be to include dummy functions in the .so file that have the same name as the target functions in the main executable but do nothing.
At load time, I think the loader will interpose the functions from the main image overriding the dummy versions in the .so.
In porting code from Windows (which does not do this) to Linux, I had several functions in the main image duplicating names of intentionally different functions in DLLs. In Windows it all works fine because nothing is exported nor imported without effort to make it exported and imported. In Linux, such things are exported, imported and interposed all by default and it took lots of effort to get those symbols to be final at link time so they couldn't be interposed. You have the opposite issue, which I think the Linux default behavior makes easier.
Mara's suggestion is the one I recommend. It is best to be explicit about a library calling into an application and the pattern for doing that is Register a Callback Function.
In C++, the easiest way to do that is to have the library define an interface (ABC) that is implemented by a class in the application. The application implements a class which implements the interface. The app registers an object of this type with the library. And the library just calls a method on the registered object.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.