In the past, most of the methods for writing dynamic link libraries were directly implemented using the extern 'C' method. However, if classes are used for writing, problems may occur, this is because the class cannot determine the entry point position during compilation. In this way, a problem occurs. How to use it? The first two methods are implemented using common function calls in linux. In vc ++, the macro is used to dynamically adjust the input and output _ declspec (dllimport) and _ declspec (dllexport) for the entry point)
Method 1: Pass the class as a parameter to the interface function:
// ----------------------- Myclass. h file -----------------
# Ifndef MYCLASS_H
# Define MYCLASS_H
Class myclass
{
Public:
Myclass (){}
~ Myclass (){}
Public:
Int sum (int a, int B );
Private:
Int _;
Int _ B;
};
# Ifdef SHARED
Int (* sum) (myclass * my, int a, int B );
# Else
Int sum (myclass * my, int a, int B );
# Endif
# Endif // MYCLASS_H
// ----------------------- Myclass. cpp file -----------------
# Include "myclass. h"
Int myclass: sum (int a, int B)
{
Int c = a + B;
Return c;
}
Int sum (myclass * my, int a, int B)
{
Int c = my-> sum (a, B );
Return c;
}
// ----------------------- My. cpp test file -----------------
# Include <iostream>
# Include <dlfcn. h>
# Define SOFILE "./my. so"
# Define SHARED
# Include "myclass. h"
Using namespace std;
Int main (int argc, char * argv [])
{
Myclass my;
Void * dp;
Char * error;
Cout <"Dynamic Link Library application demonstration" <endl;
Dp = dlopen (SOFILE, RTLD_LAZY);/* Open the dynamic link library */
If (dp = 0)/* exit if opening fails */
{
Cout <"Exit dlopen if opening fails" <endl;
Return (1 );
}
Sum = (int (*) (myclass * my, int a, int B) dlsym (dp, "sum");/* locate the sum function */
Error = dlerror ();/* check error */
If (error)/* exit if an error occurs */
{
Cout <"exit if an error occurs: locate sum function" <endl;
Return (1 );
}
Int a = 10;
Int B = 100;
Int c = sum (& my, a, B);/* call this shared function */
Cout <"c =" <endl;
System ("PAUSE ");
Return 0;
}
Compile:
G ++ myclass. h myclass. cpp-shared-o my. so
G ++ my. cpp-rdynamic-lds-o my.exe
**************************************** ******************************
Method 2: Name the global variable of a class and use this global variable in the interface:
// ----------------------- Myclass. h file -----------------
# Ifndef MYCLASS_H
# Define MYCLASS_H
/*
* No description
*/
Class myclass
{
Public:
Myclass (){}
~ Myclass (){}
Public:
Int sum (int a, int B );
Private:
Int _;
Int _ B;
};
// Name the global variable of a class and use this global variable in the interface:
Extern myclass my;
# Ifdef SHARED
Int (* sum) (int a, int B );
# Else
Int sum (int a, int B );
# Endif
# Endif // MYCLASS_H
// ----------------------- Myclass. cpp file -----------------
# Include "myclass. h"
Int myclass: sum (int a, int B)
{
Int c = a + B;
Return c;
}
Int sum (int a, int B)
{
Int c = my. sum (a, B );
Return c;
}
// ----------------------- My. cpp test file -----------------
# Include <iostream>
# Include <dlfcn. h>
# Define SOFILE "./my. so"
# Define SHARED
# Include "myclass. h"
Using namespace std;
Int main (int argc, char * argv [])
{
// Myclass my;
Void * dp;
Char * error;
Cout <"Dynamic Link Library application demonstration" <endl;
Dp = dlopen (SOFILE, RTLD_LAZY);/* Open the dynamic link library */
If (dp = 0)/* exit if opening fails */
{
Cout <"Exit dlopen if opening fails" <endl;
Return (1 );
}
Sum = (int (*) (int a, int B) dlsym (dp, "sum");/* locate the summation function */
Error = dlerror ();/* check error */
If (error)/* exit if an error occurs */
{
Cout <"exit if an error occurs: locate sum function" <endl;
Return (1 );
}
Int a = 10;
Int B = 100;
Int c = sum (a, B);/* call this shared function */
Cout <"c =" <endl;
System ("PAUSE ");
Return 0;
}
Compile:
G ++ myclass. h myclass. cpp-shared-o my. so g ++ my. cpp-rdynamic-lds-o my.exe ******************************** method 3: in the DLL output header file, use # ifdef MYLIBAPI # else # define MYLIBAPI _ declspec (dllimport) # endif In the DLL cpp file, add the following before the referenced header file: # define MYLIBAPI _ declspec (dllexport) Note that the extern "C" is no longer available. Because we want to Output C ++ class. At the same time, in the header file of the class, it is OK to write the class MYLIBAPI classname. **************************************** * ******** # Include <iostream>
Using namespace std;
Class DLLClass {
Public:
// Exported member function
_ Declspec (dllexport) void functionA (void)
{
Cout <"In Function A of the exported function" <endl;
Return;
}
};
// Exported class
Class _ declspec (dllexport) ExportDLLClass {
Public:
Void functionB (void)
{
Cout <"In Function B of the exported class" <endl;
Return;
}
};
// Exported instance of the DLLClass
_ Declspec (dllexport) DLLClass test;
Save it as TestDLL. cpp and compile it. Because the code is written using Editplus, you must manually compile it. ^
I am using VS 2005's clcompiler interface cl.exe. Under Cmd: CL/c TestDLL. cpp
In this case, TestDLL. obj is generated and then linked to link TestDLL. obj/DLL.
The TestDLL. dll, TestDll. exp, and TestDll. lib files are generated.
The work is coming to an end section ....
Start and write the calldll.exe file used to call testdll.dll:
Class DLLClass {
Public:
// Imported member function
_ Declspec (dllimport) void functionA (void );
};
// Imported class
Class _ declspec (dllimport) ExportDLLClass {
Public:
Void functionB (void );
};
// Imported instance of the DLLClass
_ Declspec (dllimport) DLLClass test;
Int main (void)
{
ExportDLLClass TestClass;
Test. functionA ();
TestClass. functionB ();
Return 0;
}