Using static libraries and dynamic libraries under windows

Source: Internet
Author: User
Tags export class mul

Using static libraries and dynamic libraries under windows (i) Environment

Windows 7 32-bit
Visual Studio 2013

(ii) Introduction to DLLs and Lib

These concepts are well understood under Windows because the general steps when you need to introduce a dynamic library (DLL) or a static library (Lib) are:

Add header File
Configuration header File Directory
Directory of the Configuration library
Copy the DLL to the directory where the executable file resides (dynamic libraries only)

Therefore, it is good to understand the relationship between. h files,. dll files, and. lib Files:

The. h file is required at compile time because it has a function or variable declaration.
A. dll file is a built-in dynamic library that is dynamically loaded when the program is run.
And the. lib file has to say, first of all, for a static library, It is in the form of Xxx.lib, and for a dynamic library, when the IDE helps us generate a. dll file, it also generates a Xxx.lib file, where the. Lib is not a static library, but an import library for the dynamic link library (import libary)

(iii) Use of static libraries under Windows generate a static library (1) Create the WIN32 project in VS2013, create a post-compiler pop-up option box, and select the static Library project.

(2) writing. h files and. cpp files, respectively
//staticmath.h#pragma onceclassstaticmath{ Public: Staticmath (void); ~staticmath (void);Static DoubleAddDoubleADoubleb);Static DoubleSubDoubleADoubleb);Static DoubleMulDoubleADoubleb);Static DoubleDivDoubleADoubleb);};
//StaticMath.cpp#include "StaticMath.h"double StaticMath::add(doubledouble b){    return a + b;}
(3) Right-click Project--Build

A static library is generated under the Debug directory of the project.

using a static library (1)Create a console application and write the test code: * *
//testStatic.cpp#include <iostream>#include "StaticMath.h"int main(){    double1.2;    double2.4;    std::cout"a+b="<<StaticMath::add(a, b)<<std::endl;    system("pause");    return0;}
(2) configuration Items

Configuration header File Reference directory: Project Properties->c/c++-> General, add-in directory with static library header files, including directory.

To configure a static library's directory: Add an absolute path to a static library, other options, command line, link, project properties, and so on

(3) test static library

Run the test project directly and the results are as follows:

(iv) Use of dynamic libraries under Windows

Using a dynamic library under Windows is much more complex than a static library.

Create a dynamic library(1) Create the WIN32 project in VS2013 and select the dynamic library, then the system will automatically help us do something: for example, will write the DllMain function as the initialization of the portal, such as initializing. h and. cpp files, with sample export functions and export class declarations. We just need to make a few additions on top of it:
//dynamicmath.h//The following ifdef blocks are created to make it easier to export from a DLL//The standard method of the macro. All files in this DLL are defined on the command line Dynamicmath_exports//symbol compiled. In the use of this DLLThis symbol should not be defined on any other item. In this way, any other items in the source file that contain this file willThe //Dynamicmath_api function is considered to be imported from a DLL, and this DLL will be defined with this macrothe//symbol is considered to be exported. #ifdef dynamicmath_exports#define DYNAMICMATH_API __declspec (dllexport)#else#define DYNAMICMATH_API __declspec (dllimport)#endif//This class is derived from DynamicMath.dll.classDynamicmath_api Cdynamicmath { Public: Cdynamicmath (void);//TODO: Add your method here.     Static DoubleAddDoubleADoubleb);Static DoubleSubDoubleADoubleb);Static DoubleMulDoubleADoubleb);Static DoubleDivDoubleADoubleb);};//Example export variable declarationexternDynamicmath_apiintNdynamicmath;//Example export function declarationDynamicmath_apiintFndynamicmath (void);

in the compiler-generated template, the Cdynamicmath class has been decorated with dynamicmath_api (exported from DynamicMath.dll), so member functions in the class no longer need to be dynamicmath_api decorated .

///DynamicMath.cpp: Defines the export function of the DLL application. //#include "stdafx.h"#include "DynamicMath.h"//This is an example of exporting a variableDynamicmath_apiintNdynamicmath=0;//This is an example of an exported function. Dynamicmath_apiintFndynamicmath (void){return  the;}//This is the constructor for the exported class. //For information about class definitions, see DynamicMath.hCdynamicmath::cdynamicmath () {return;}//I added the Export function myselfDoubleCdynamicmath::add (DoubleADoubleb) {returnA + b;}

The build that right-click Project will be generated under the Debug directory:

DynamicMath.dll (dynamic link library)
DynamicMath.lib (import library for dynamic link libraries)

With the. h file, the dynamic link library, the three-piece guide, we can easily statically load (that is, implicitly linked way) dynamic link library. Otherwise, you need to LoadLibrary into the DLL file, and then manually getprocaddress get the corresponding function (also known as explicit link).

The following is a reference: http://blog.csdn.net/yusiguyuan/article/details/12649737 collation of some information.

With the import library, you only need to link to the import library and follow the declaration of the header file function interface to call the function. the difference between an import library and a static library is that they are essentially different things. the static library itself contains the actual execution code, symbol table and so on, and for the import library, its actual execution code is located in the dynamic library, the import library contains only the address symbol table, etc., to ensure that the program to find some basic address information of the corresponding function.

The General Dynamic Library program has lib files and DLL files. lib files must be connected to the application at compile time, and DLL files are not called until the run time. If there are DLL files, then the corresponding LIB file is generally some index information, specifically implemented in the DLL file. If only the Lib file, then the Lib file is statically compiled, the index and implementation are in it. A statically compiled LIB file has the advantage of not having to hang up the dynamic library when installing to the user. But there are drawbacks, that is, the application is larger, and the flexibility of the dynamic library is lost, and when the version is upgraded, a new application will be published at the same time. in the case of a dynamic library, there are two files, and one is the introduction library (. LIB) file, a DLL file that contains the name and location of the function exported by the DLL, the DLL contains the actual functions and data, the application uses the Lib file to link to the DLL file that is needed, the functions and data in the library are not copied to the executable file, Therefore, in the application's executable file, it is not the called function code, but the memory address of the function to be called in the DLL, so that when one or more applications run, the program code and the called function code are linked together, thus saving memory resources. As you can see from the instructions above, DLLs and. lib files must be released with the application or the application will produce an error.

working with Dynamic libraries

Here's how to use implicit chaining (static loading).

(1) Create a console application and write test code
 #include " DynamicMath.h "  #include <iostream>  int  Main () {int  num = Fndynamicmath (); //auto-generated example export function  std ::     cout  << num << std :: Endl;    double  a=1.2 ;    double  b=2.5 ; std :: cout  << Cdynamicmath::add (A, B)    << std :: Endl;    System ( "pause" ); return  0 ;}  
(2) configuration Items

Configuration header File directory: Project Properties->c/c++-> General-, additional Include directories

Configuring the Import library (additional Dependencies) directory: add-on library directory, general--, linker, project properties
In addition to this, you need to configure specific additional dependencies. lib: Add "DynamicMath.lib" in "Additional Dependencies", type-I, linker-I, Configuration properties

Configure Dynamic link Library directory: Project properties, configuration Properties->vc++ Directory

(3) test

Compile run Test Dynamic Library project, found error: said the system cannot find the DynamicMath.dll file , in fact, we need to copy the DLL files to the debug directory of the test project after normal operation:

(v) Reference

Http://www.cnblogs.com/houkai/archive/2013/06/05/3119513.html
Http://www.cnblogs.com/skynet/p/3372855.html
http://blog.csdn.net/yusiguyuan/article/details/12649737
http://blog.csdn.net/zhangxiao93/article/details/51344625

Using static libraries and dynamic libraries under 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.