Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC static export
static export error:
in test1.dll:
xxx.h
 
    class AFX_EXT_CLASS CTest1
    {
        static int num;
    }
xxx.cpp
 
 
    int CTest1::num = 0;
in a cpp of test2.dll: test2 is dependent test1.dll
 
    ...
    int i = CTest1::num;
    ...
in a cpp of App: App is dependent test1.dll & test2.dll
 
    ...
    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 29-Apr-13 4:41am
pengm348
Edited 29-Apr-13 5:11am
_duDE_8.1K
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Probably you have to have a declaration in your executable which uses DLL, like this:
declspec( __dllimport ) extern int MyClass:: num;
If it does not help, then add a static helper function for accessing that variable, for example:
AFX_EXT_CLASS class CTest1
{
    static int num;
public:
    static int & GetStaticVariable();
}
 
// in CPP file:
int & CTest1::GetStaticVariable()
{
    return myStaticVariable;
}
  Permalink  
v2
Comments
Ian A Davidson at 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.
du[DE] at 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 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
#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.
  Permalink  
v2
Comments
H.Brydon at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Maciej Los 359
2 Kornfeld Eliyahu Peter 309
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100