none
Exporting const static data members to dll

    Question

  • Hello there,

    I am using VC++ 2003. I have a class somewhat like this:

    // C.h
    class __declspec(dllexport) C {
    public:
        const static float CVALUE;
        static void foo();
    };

    // C.cpp
    const static float C::CVALUE = 10.0f;
    void C::foo() { }

    When I attempt to call C::foo() from an external program using my dll everything is fine. When I attempt to access C::CVALUE I get a linker error like this:

    error LNK2001: unresolved external symbol "public: static float const C::CVALUE".

    I checked the dll with Depends.exe and it seems that the symbol C::VALUE is being exported. Do you guys have an idea of what I could do to solve this problem?

    Thanks,

    William
    Wednesday, March 29, 2006 11:05 PM

Answers

  • Hint: declare your static variable as __declspec(dllimport) on the client side.
    Friday, March 31, 2006 2:48 AM
    Moderator
  • You can not export data fields from a DLL. You can only export functions!

    The reason is simple. The binding mechanism for function to a DLL have a jump table where the final reference from the DLL is set into.

    Because the load address is unknown it is not possible to unreference a variable in a DLL.

    Write a function that returns that static value!

    Thursday, March 30, 2006 3:52 PM
    Moderator

All replies

  • You can not export data fields from a DLL. You can only export functions!

    The reason is simple. The binding mechanism for function to a DLL have a jump table where the final reference from the DLL is set into.

    Because the load address is unknown it is not possible to unreference a variable in a DLL.

    Write a function that returns that static value!

    Thursday, March 30, 2006 3:52 PM
    Moderator
  • I suspect that your answer is wrong, because:

    (1) I have seen and used a DLL that does exactly what I described.
    (2) The data fields do appear in the DLL (can be seen with Depends.exe)

    Martin, if you have an idea of how the system I told you about sorted this out, please tell me. Writing the function is a solution, but it isn't as elegant as having the data fields exported. :)

    If anybody has a hint, please write it.

    Thanks
    William
    Friday, March 31, 2006 1:47 AM
  • Hint: declare your static variable as __declspec(dllimport) on the client side.
    Friday, March 31, 2006 2:48 AM
    Moderator
  • We can use data segments to share variable in a dll.

    #pragma data_seg("SHARED")

    float C::CVALUE= 0;

    #pragma data_seg()

    http://www.codeproject.com/Articles/240/How-to-share-a-data-segment-in-a-DLL


    Thanks, Renjith V R


    Tuesday, June 05, 2012 1:26 PM
  • You are totally wrong. Using exclamation marks does not make your response any better. Go study instead and do not mislead others when you do not have proper knowledge.
    Friday, November 29, 2013 11:13 AM
  • Which one is wrong here?

    Thanks, Renjith V R

    Saturday, November 30, 2013 9:23 AM
  • Martin Richter answer is wrong, you can certainly export static fields from a dll using dllexport/dllimport: http://support.microsoft.com/kb/90530/en-us

    Your answer has nothing to do with the question, shared sections are supposed to be used to shared data between processes, they're not an alternative to dll import/export. I'm have no idea why you even posted it given that this thread is years old.

    Saturday, November 30, 2013 10:34 AM
    Moderator