Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
static export error:
in test1.dll:
xxx.h
C++
class AFX_EXT_CLASS CTest1
{
    static int num;
}

xxx.cpp

C++
int CTest1::num = 0;

in a cpp of test2.dll: test2 is dependent test1.dll
C++
...
int i = CTest1::num;
...

in a cpp of App: App is dependent test1.dll & test2.dll
C++
...
int i = CTest1::num;
...


In test2.dll, "Unresolved external symbol" on static: CTest1::num. but in App, no problem.
BTW:if i use other function of CTest1, no problem.
Posted
Updated 29-Apr-13 4:11am
v2

Probably you have to have a declaration in your executable which uses DLL, like this:
C++
declspec( __dllimport ) extern int MyClass:: num;

If it does not help, then add a static helper function for accessing that variable, for example:
C++
AFX_EXT_CLASS class CTest1
{
    static int num;
public:
    static int & GetStaticVariable();
}
 
// in CPP file:
int & CTest1::GetStaticVariable()
{
    return myStaticVariable;
}
 
Share this answer
 
v2
Comments
Ian A Davidson 29-Apr-13 10:27am    
+5.
I cannot confirm either if the declspec declaration will work - I was trying to find if I had an example, but surprise, surprise, I don't.
I think I'd go with the latter option anyway, as you can better hide your implementation.
Ian.
Leo Chapiro 29-Apr-13 10:55am    
>I think I'd go with the latter option anyway, as you can better hide your implementation. I agree, the second option is better in terms of OOP. Thanks for upvoting BTW :)
The issue is likely that because you have a DLL importing symbols from another DLL, and that all of the symbols are being marked for export as du[DE] explained above.

What you need are conditional declarations dependant on each DLL, so that they won't be mistakenly interpreted incorrectly by other DLLs.

Modify your header to look something like this.

C++
#ifdef DLLTEST1_EXPORTS
#define DLLTEST1_API __declspec(dllexport)
#else
#define DLLTEST1_API __declspec(dllimport)
#endif

class DLLTEST1_API CTest1 {
public:
	CTest1(void);
	// TODO: add your methods here.
};


Then define DLLTEST1_EXPORTS in either your Test1 project or in the stdafx.h for that project.
 
Share this answer
 
v2
Comments
H.Brydon 29-Apr-13 18:04pm    
Not correct - the problem is not export of the class.

The class's static variable needs to be exported.

See Solution #1.
Philippe Mori 29-Apr-13 18:49pm    
The definition need to be different when compiling first DLL (export) and second DLL (import) otherwise it won't works.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900