Exporting const static data members to dll
-
Wednesday, March 29, 2006 11:05 PMHello 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
All Replies
-
Thursday, March 30, 2006 3:52 PMModerator
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!
-
Friday, March 31, 2006 1:47 AMI 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 2:48 AMModerator
Hint: declare your static variable as __declspec(dllimport) on the client side. -
Tuesday, June 05, 2012 1:26 PM
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
- Edited by Renjith V RamachandranMicrosoft Community Contributor Tuesday, June 05, 2012 1:27 PM

