Shared library design problem

This is a discussion on Shared library design problem within the C++ Programming forums, part of the General Programming Boards category; Hi, I wrote a program that load some shared library I also wrote. My problem is that the library implements ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    Question Shared library design problem

    Hi,

    I wrote a program that load some shared library I also wrote.
    My problem is that the library implements an abstract class and the program has the the .h so it can use the library object. The thing is that I want it to be both ways.
    I want to be able to give the library a object of the main program (as a pointer) and be able to use it from the library but I don't want to have de .cpp of that class at compile time when I compile the lib.

    Can I do this? any ideas?

    Thanks!

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    As long as the library has access to the appropriate header files, then yes, you can do this. Since you say "shared library" and not "DLL" I'll assume you mean UNIX. You need to link your main program using the --export-dynamic flag to the linker which will cause its global symbols to be made available for other dynamic code. So the shared library can use dlsym() to obtain function pointers from the main program.

    If you are using gcc to link, you can pass "-Wl,--export-dynamic" (no spaces)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Thanks!

    I'll try this out later.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Why do you want a library to be dependent on an application? Isn't that kind of backwards?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by medievalelks View Post
    Why do you want a library to be dependent on an application? Isn't that kind of backwards?
    It's not uncommon, especially on Windows. It makes more sense if you think "shared object" instead of "shared library." Not every dynamic component need be thought of as a library. Consider an application plug-in. This plug-in probably needs to call the parent application in various ways. This could be done through a set of function pointers, but it could also be done directly if the applications exports dynamic symbols.

    Whether you provide component linkage through function pointers or direct calls, you still must maintain a compatible API between the components. So as far as code architecture there is no strong argument either way.

    For whatever reasons (probably philosophy), the "application-as-collection-of-dynamic-components" concept is uncommon on UNIX systems, but the support for it is there.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    The idea is exactly for a plugin.
    But if there's any better way of doing this please tell me. I don't really know much about plugins and such.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by p1r0 View Post
    The idea is exactly for a plugin.
    But if there's any better way of doing this please tell me. I don't really know much about plugins and such.
    You could use --export-dynamic, but you need to think carefully about what you allow the plug-ins to call. By exporting your application's symbols, you allow the plug-in to call any function in the application that it wants. That's dangerous, because somebody may develop a plug-in which becomes wildly popular that uses some function inside your application that you want to eliminate. Now you can't, because this popular plug-in requires it.

    You might want to consider some kind of class which proxies for the application and exposes only a limited set of functionality. When the application initializes the plug-in, it can pass an instance of this class, and the plug-in will interact with the application only through this class.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    You might want to consider some kind of class which proxies for the application and exposes only a limited set of functionality. When the application initializes the plug-in, it can pass an instance of this class, and the plug-in will interact with the application only through this class.
    This is what I wanted to do, bunt couldn't find out how to without providing the implemantation for that class to the plugin ant compile time, and that is something I don't wanna do either.
    I had and abstract class for this, but could not use objects of that type as arguments for the method of the plug in that would get an instance of the proxy.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by p1r0 View Post
    This is what I wanted to do, bunt couldn't find out how to without providing the implemantation for that class to the plugin ant compile time, and that is something I don't wanna do either.
    I had and abstract class for this, but could not use objects of that type as arguments for the method of the plug in that would get an instance of the proxy.
    Why not? You can pass abstract pointers/references and still use the underlying object.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by brewbuck View Post
    Why not? You can pass abstract pointers/references and still use the underlying object.
    I'm really ashamed about this: I just tried my code to send you the exact error the compiler gave me and as expected it work. I really don't know what I was doing yesterday with this.

    Thank you very much for your help. I'll try to make the plug in this way as it is the best way to do it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shared library, but not dynamic !?
    By ccox421 in forum C Programming
    Replies: 11
    Last Post: 03-17-2009, 01:03 PM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  3. problem with sending array of struct over shared memory
    By jet-plane in forum C Programming
    Replies: 26
    Last Post: 05-10-2008, 04:10 AM
  4. Problem With WinPcap library
    By DrMario in forum C Programming
    Replies: 0
    Last Post: 03-26-2005, 10:26 AM
  5. Im a Newbie with a graphics design problem for my simple game
    By Robert_Ingleby in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2001, 05:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21