exporting a function from a static library.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Androidlava
    New Member
    • Nov 2011
    • 5

    exporting a function from a static library.

    I am interested in using my static lib to create a dll (implicitly linking). which means I need to (in vs2008) create a dll project that should generate the following:

    1. header file (which have exported function declarations. These are simple wrappers to actual functions in the static lib and are using __declspec(dlle xport) which are in the dll centric .cpp file)
    2. import lib which will be made as a result of creating the dll
    3. the actual dll which is created.

    Now this is what I have:
    I have made a test program that will utilize the above dll(including the import lib/header files) to test it. in this I have included all the three items. now the exe compiles/links without issue. however in the main.cpp when i call the exported functions they seem to see the exported dlls...but can't seem to execute the static library functions which they wrap.

    question:
    1. how do I get them to see those functions?
    2. is there an easier way to do this such that I still share my static library but now if anyone wants to create a dll they can because I have added "the exported function capability" to my static lib? OR do I just need to provide them with a imported library from my dll which has all my static library obj files as well? (i thought I did that when i made my dll but maybe just adding that as a reference to create the dll in vs2008 does not work??


    update: i was able to use the extern C in the exe to finally get to see the dll function being exported.

    so, the wrapper works in the sense that i can now call it in the exe but i am not able to execute the static lib function inside that wrapper. i have provided a sample of one of the wrappers below


    thanks
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Show us one of your wrapper functions.

    Comment

    • Androidlava
      New Member
      • Nov 2011
      • 5

      #3
      //wrapper in dll .cpp file

      extern "C" __declspec(dlle xport) bool code_generate_d ll(const char* seed, char * output)
      {
      //making call to static lib function
      return code_gen(seed,o utput);

      };


      above is an example of one of the functions in the dll implementation.

      i was researching and came across this...


      would this apply to me then? if so how do i implement the linker export in my case. I am just trying to do due diligence as much as i am able to resolve this issue.

      thanks

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        I do not think that knowledge base article applies to you at all. That is talking about exporting functions in a static library directly from a DLL but you are not doing that you are wrapping the functions in the static library. The function should get included when you compile the DLL since it should be unable to compile without including them.

        What do you mean by "can't seem to execute the static library functions"?

        Presumably you create a program that imports your DLL and calls code_generate_d ll. So what happens when you compile/run that program?

        Comment

        • Androidlava
          New Member
          • Nov 2011
          • 5

          #5
          so basically what happens is that it sees the exported dll function code_generate_d ll and executes it (i put in some couts in there which work. however when i place the static library function which it is supposed to call then it that bit of code is not working. in other words the static library function that I am trying to indirectly export via the wrapper in the dll is not executing).

          so the exe can see the exported code_generate_d ll. it executes some of the instructions there but won't exe the static library function. that is the part that baffles me. if i was able to create the dll WITH the static library linked how come the dll wrapper function can't execute the static library function call.


          i just wanted to make sure you understood. my wrappers which i need to export are in dll. so the implementation is there and in the dll i am calling the static library function. seems to me there is a problem where the dll can't seem to link to the static lib. in vs2008 i just drag and drop the static lib and thats how it works.

          i also removed the middle man dll and just directly called the static lib from the exe and that works when i run it with the static lib directly. so the lib is good as well..

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            This sounds odd if the static library is called in the DLL either
            • The DLL with fail of link if the static library is unavailable for some reason
            • When the DLL function is called from your program it will invoke the static library function.


            But, if I understand, you are saying that the DLL builds but the static library function is not called.

            You haven't said how you know the static library isn't called.

            I would say it is time to break out the debugger and step through the code of the dll as it runs, see what happens when it gets to that function call.

            Comment

            • Androidlava
              New Member
              • Nov 2011
              • 5

              #7
              ok so i am stupid. but this is how you learn i guess. basically i was making changes and not updating my dll file that the exe was using....i hate myself!!!!!! in other words the dll file that the exe was using was an older one and not the latest one.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                That sort of error catches out everyone every now and then.

                Expect to make it once every 2 - 3 years for the rest of your life :D

                Comment

                • Androidlava
                  New Member
                  • Nov 2011
                  • 5

                  #9
                  i can't believe it!!! but man tough lesson. heheh

                  Comment

                  Working...