Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
Hello, let's say I have a master application which loads other plugins (.so dynamic libraries). When the plugin is loaded (dlopen) and its function "Load" is called by the master ...
  1. #1
    Just Joined!
    Join Date
    Jun 2008
    Posts
    13

    Question How should a plugin call its parent's function

    Hello,
    let's say I have a master application which loads other plugins (.so dynamic libraries). When the plugin is loaded (dlopen) and its function "Load" is called by the master application, the plugin should register its services in the master application.
    So let there be a vector of services and a function "RegisterService" in the master application (files Services.h and Services.cpp). Function "RegisterService" registers a new service and updates the vector of services. How should a plugin call this "RegisterService" function and how should I compile it?
    The problem is that if I don't compile plugin.cpp with Services.cpp, then during loading of the plugin there is an unresolved link to RegisterService call. If I compile it with Services.cpp, then these two functions (RegisterService) are two different functions and I cannot update the vector in master application.

    Hope it's at least a little bit understandable.
    Thanks for answers.

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    Could you post your code and make file...Thanks

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    13
    It's a little bit more of code so I put it as an attachment. In this configuration in runs but the plugin's service is not registered.
    Attached Files Attached Files

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    #!/bin/sh
    # first compile plugin
    g++ -fPIC -g -c -Wall Services.cpp plugin.cpp && g++ -shared -o plugin.so Services.o plugin.o
    # then compile master app
    g++ -o main main.cpp Services.cpp -ldl
    Why are you compiling Services.cpp into g++ -o main main.cpp Services.cpp -ldl and then compiling Services.cpp as a shared object g++ -shared -o plugin.so Services.o plugin.o?
    Ooops you explain that in the initial post

  5. #5
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I tried compiling your program and had an error in int main (int argc, char ** argv[])

    so I changed it to int main (int argc, char ** argv) and it compiled and ran...by ran I mean the program ran to completion without crashing

  6. #6
    Just Joined!
    Join Date
    Jun 2008
    Posts
    13
    Quote Originally Posted by gerard4143 View Post
    the program ran to completion without crashing
    Sure it does. But at the end you saw "NO ". I want there "YES!".

  7. #7
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    This is the code in plugin.cpp

    Code:
    #include "plugin.h"
    
    void Load (void)
    {
       printf("Loading plugin...\n");
       RegisterService("LS_PLUGIN",NULL);
    }
    You make a call to RegisterService here...thats why you have to include Services.cpp while compiling the shared object or you get an "unresolved link to RegisterService call"

    Basically the shared object can't resolve an address for RegisterService without compiling Services.cpp into it....

  8. #8
    Just Joined!
    Join Date
    Jun 2008
    Posts
    13
    So it means that dynamically linked library cannot use a function provided by its "parent"? The unresolved links in the library cannot be "bound" to the provided function?

  9. #9
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    You might and I stress might be able to pass the shared object a pointer to the the function in the host program...Hope this helps

  10. #10
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    I wrote a very small and simple shared object that receives a function pointer from the host program an it did work, it called and executed the host programs function without incident. The thing to stress is very small and simple shared object I'm not sure how this solution would work with a complicated function ...Hope this helps

    the function I tried was:

    Code:
    void printthis()
    {
    	fprintf(stdout, "hello world!\n");
    }
    Like I said a simple function...and all in all its a very complicated solution even for a simple function

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...