Of course, you merely have to declare the function in the library.
Up vote 5 down vote favorite share g+ share fb share tw.
When we have an exe or dll and a static library attached to it, we are able to use extern keyword to access static library's variables and/or functions from the exe or dll. To make things simpler, let's assume ve have an exe and a lib attached to it. What I am trying to do is to call a function of exe from lib.
Executable Code void doSomething() { // do something here } Static Linked Library Code void onSomeEvent() { doSomething(); // call doSomething() here } Vice versa is easy but I wonder if this can be done in a way like extern keyword. Or what is the best method? What comes to my mind is to pass a function pointer (like void*) to one of the functions / methods in the lib (probably to a class constructor).
I think this should work but I don't want to touch library's code too much since library is not mine and can be replaced with newer versions. I can add/remove a few lines of code to it but I want to prevent from changing function interfaces. What is the better way?
C++ c static-linking extern link|improve this question asked Nov 21 '11 at 14:35Emir Akayd? N3,530425 100% accept rate.
This is highly platform specific. What compiler/toolchain do you use? – sehe Nov 21 '11 at 14:40.
Of course, you merely have to declare the function in the library. Void onSomeEvent() { void doSomething(); // declares the function doSomething(); // call doSomething() here }.
Strangely worked very well. :) I know what prototype is but I just met its usage like this one. Thanks so much dude.
– Emir Akayd? N Nov 21 '11 at 14:52.
You may also declare it outside your function, you may have other functions need it. Void doSomething(); // declares the function void onSomeEvent() { doSomething(); // call doSomething() here } void onSomeEvent2() { doSomething(); // call doSomething() here }.
Given the static library is (probably) meant to be used in many different programs, it's not uncommon to use the callback approach where the exe initialises the library and passes it one or more function pointers to use to do things (like logging messages, or whatever). If the exe doesn't pass in the function pointers (or passes them as NULL) then the library can simply not call those functions and the library will work well in both environments. This is much better than assuming the functions are always defined in the exe.
Thank you for your comment but in my case, avakar's answer is exactly what I was looking for. Though you are right about you points. – Emir Akayd?
N Nov 21 '11 at 14:59.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.