I have not found any program on the Internet to package classes into DLL files. Today I wrote a test program for your reference.
1. generate DLL files of the class
1. I tested it on vs2008 and set up a project. When selecting the type of project to be created, check the DLL in application type;
2. Add a header file named mydll. H. This header file is the interface file used for testing. The Code is as follows:
# Ifndef _ mydll_h _ # DEFINE _ mydll_h _ # ifdef mylibdll # define mylibdll extern "C" _ declspec (dllimport) # else # define mylibdll extern "C" _ declspec (dllexport) # endifclass _ declspec (dllexport) testdll {// The key is in this place. If an error occurs in this place, the private: int A; public: cannot be used for the DLL file you created: testdll (); void Seta (); int geta () ;};# endif
3. Add a source file named mydll. cpp. This is the implementation file of the class:
#include "stdafx.h"#include <iostream>#include "mydll.h"using namespace std;testDll::testDll(){cout<<"test dll"<<endl;a = 11;}int testDll::getA(){return a;}void testDll::setA(){a = 33;}
4. All other files are automatically generated by vs2008 and do not need to be modified. Compile the file to generate the DLL and Lib files;
2. Test the self-generated DLL and Lib files
1. Create a project. Select the build EXE application type;
2. Copy the generated DLL and Lib files to this project directory, and copy mydll. h (Key);
3. Forget that in vs2008, add the DLL and Lib directories in linker, and add the Lib names to addtional dependencies;
4. Add the following code to the main program of the test file:
#pragma comment(lib, "dllOne.lib")#include "stdafx.h"#include <iostream>#include "mydll.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){testDll* tmp = new testDll();cout<<tmp->getA()<<endl;tmp->setA();cout<<tmp->getA()<<endl;getchar();return 0;}
4. Run and test.