Create and use static libraries and dynamic libraries in Windows

Source: Internet
Author: User

I. Creation and use of static libraries

1. Creation of a static library

(1) In VS2013, select the menu->file->new->project, select the Visual C + +->win32 option, then click the Win32 Project icon and select Win32 Application Wizard, select the static library option under Application type. The project name is Teststaticlib.

(2) Create a new Test_header.h header file under the header files of the Solution Explorer

1 #ifndef Test_header_h_2 #defineTest_header_h_3 4 #ifdef _WIN325 #ifdef _lib6 #defineDECLSPEC __declspec (dllexport)7 #else8 #defineDECLSPEC __declspec (dllimport)9 #endifTen #else One #defineDECLSPEC A #endif -  - #endif

The __declspec modifier is only valid under the Windows platform.
The role of __declspec (dllexport) is to export the current function or class so that it can be called from the DLL.

The role of __declspec (dllimport) is to use exported DLL functions or classes in your application.

When you create a new static library project in the VS2013 compiler, the _LIB macro is predefined, so the above code

#define DECLSPEC __declspec (dllexport) is effective.

(3) Create a new Source.h file under the header files of the Solution Explorer

1 #ifndef Source_h_2 #defineSource_h_3#include"Test_header.h"4 5DECLSPECintMyFunction (intAintb);6 7 classDECLSPEC MyClass8 {9  Public:TenMyClass (intAintb): M_a (a), M_b (b) {} One~MyClass (); A     intAdd (); - Private: -     intm_a; the     intM_b; - }; - #endif

(4) Create a new Source.cpp file under the Source Files folder of the Solution Explorer

1#include"Source.h"2 3 intMyFunction (intAintb)4 {5     returnA +b;6 }7 8myclass::~MyClass ()9 {Ten } One  A intMyclass::add () - { -     returnM_a +M_b; the}

(5) Compile the project, the TetsStaticLib.lib static library file will be generated under the Debug folder of the project catalog.
2. Use of static libraries

(1) Create a new WIN32 console project, named Testlib.

Set project properties for Testlib

①configuration properties->c/c++->general->additional Include Directories

D:\program\c++pro\teststaticlib\teststaticlib

②configuration properties->linker->general->additional Library Directories

D:\program\c++pro\teststaticlib\debug

③configuration properties->linker->input->additional Dependencies

TestStaticLib.lib

(2) Add the following test code

1#include"stdafx.h"2#include"Source.h"3#include <iostream>4 using namespacestd;5 6 7 int_tmain (intARGC, _tchar*argv[])8 {9     intc = MyFunction (1,2);Tencout << C <<Endl; One  AMyClass CLA (2,4); -cout << Cla.add () <<Endl; -System"Pause"); the     return 0; -}

(3) Compile and run the generated TestLib.exe, you can see the results of the program, then copy the TestLib.exe to any folder can also run.

Create and use static libraries and dynamic libraries in Windows

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.