Thread: Shared Library Not Executing Function

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    23

    Shared Library Not Executing Function

    I know this is kinda lazy, but this was a long post and I don't really feel like rewriting it and taking an hour, so if you could bear with me and read...
    http://www.linuxquestions.org/questi...nction-608392/

    You'll see the problem I'm having. Any chance you guys can help?

    Thanks,
    Shane

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think it's good of you to post a link to your other post, rather than replicate the post here - but it's still a kind of cross posting, which leads to this:
    1. This question probably should be in the Linux section.
    2. What makes you think this forum is better at answering a Linux question here than on a board that is SPECIFICALLY for Linux?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Well, because this is more of a programming issue than a Linux issue (or at least I think it is), and while that is a Linux forum with a SECTION for programming, this is programming forum with a SECTION for Linux. I figure you guys might know better than they would, seeing as (I would assume) all of the user base is made up of programmers, whereas there only a certain percentage is.

    Shane
    Last edited by Osiris990; 01-04-2008 at 05:41 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    1. Moved to correct forum.

    > but it's still a kind of cross posting
    2. It's the more acceptable kind, since Osiris990 has actually waited for several days without reply before trying the next forum on the list. It's the ones who broadcast in several places at the same time which are the real bane.

    3. A 1-function main() calling a 1-function DLL, and showing how you linked the various components.
    Code:
    $ cat bar.c
    #include <stdio.h>
    #include <dlfcn.h>
    
    void myfunc ( void (*fn)(void) ) {
      printf("In dl func, calling back\n" );
      fn();
      printf("In dl func, done\n" );
    }
    
    $ cat foo.c
    #include <stdio.h>
    #include <dlfcn.h>
    
    void callback ( void ) {
        printf("In callback\n");
    }
    
    int main() {
        void *lib = dlopen("bar.so",RTLD_NOW);
        if ( lib ) {
            void *f1 = dlsym(lib,"myfunc");
            void (*func)(void (*)(void)) = (void(*)(void (*)(void)))f1;
            func(callback);
            dlclose(lib);
        } else {
            printf("&#37;s\n",dlerror() );
        }
        return 0;
    }
    
    $ gcc -o bar.so bar.c -shared
    $ gcc foo.c
    $ ./a.exe
    In dl func, calling back
    In callback
    In dl func, done
    Experiment with simple ideas, then try to integrate them into your main application.
    Also, experiment with the major ideas you're likely to need in the whole project before trying to actually use them.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    I understand what needs to be done, but I'm left with a question still... How can I get that to fit into what I'm trying to accomplish? I mean... It seems highly impractical to expect every function definition in every module to include 20 billion callbacks to functions in my libraries, so... I dunno if what I'm asking really makes any sense. Do you get what I'm saying?

    Thanks,
    Shane

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Quote Originally Posted by GNU LD Manual
    --export-dynamic
    When creating a dynamically linked executable, add all symbols to the dynamic symbol table. The
    dynamic symbol table is the set of symbols which are visible from dynamic objects at run time.

    If you do not use this option, the dynamic symbol table will normally contain only those symbols
    which are referenced by some dynamic object mentioned in the link.

    If you use dlopen to load a dynamic object which needs to refer back to the symbols defined by
    the program, rather than some other dynamic object, then you will probably need to use this
    option when linking the program itself.


    You can also use the version script to control what symbols should be added to the dynamic symbol
    table if the output format supports it. See the description of --version-script in VERSION.
    I started with these commands.
    Code:
    gcc -g -ansi -o circbot circbot.c circbot_irc.c circbot_settings.c -ldl -lpcre
    gcc -g -ansi -o modules/test.so test.c -shared -Wl,--just-symbols=circbot
    It didn't produce any warnings, but neither did it run (or didn't seem to at any rate, it could just be cygwin). At any rate, perhaps it will provide some clues.

    GNU LD certainly seems capable of doing what you ask, it's just a matter of getting the right options. But tiredness (and flu) is slowing me down .

    I understand what it is you're trying to do (.exe depends on .so, and .so depends on .exe).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Hmm. I think I get where you're going with this. I'll toy around with the linker a bit and see if I can't get it to link in the symbols from the main program.

    Also, hope you feel well soon! The flu is a <expletive apparently deleted>. =/

    Thanks,
    Shane

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Well, compiling on my system, when I add the switch to the linker to include the symbols from the main program, I get the following error.

    Code:
    bash-3.1$ ./build.sh
    /usr/lib/gcc/i486-slackware-linux/4.1.2/../../../../i486-slackware-linux/bin/ld: modules/test.so: undefined versioned symbol name stderr@GLIBC_2.0
    /usr/lib/gcc/i486-slackware-linux/4.1.2/../../../../i486-slackware-linux/bin/ld: failed to set dynamic section sizes: Bad value
    collect2: ld returned 1 exit status
    bash-3.1$
    I have no idea what that means. o_o

    Thanks,
    Shane

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    23
    Anyone have any clue what that error is all about? I can't figure it out, and all the searching I've done has turned up zilch. =/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. shared library for QNX
    By ReeV in forum C Programming
    Replies: 3
    Last Post: 05-06-2009, 10:58 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM