C ++ package classes into DLL interface files

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.