Windows library creation and use: static library + dynamic library, windows static library

Source: Internet
Author: User
Tags export class

Windows library creation and use: static library + dynamic library, windows static library
Create and use windows libraries: static libraries + dynamic libraries

 

I. creation and use of static databases 1. Creation of static databases

(1) first create a project test. The test code is as follows:

1) test. h

Void test_print ();

2) test. cpp

# Include "test. h"

# Include <stdio. h>

Void test_print ()

{

Printf ("test_print in static lib .");

}

3) Right-click the project test: properties --> Configuration properties --> General --> configuration type: Select static library lib

4) compile the project (build), and test. lib will be generated under Debug in the solution.

 

2. Use static library = lib file + header file

1) create a new project uselib

2) Click the resource file --> Add existing items --> Add the generated library test. lib.

3) Right-click Properties --> VC ++ directory --> include directory: add the directory where test. h is located

The project code is as follows:

1) main. cpp

# Include "test. h"

# Include <stdio. h>

Int main ()

{

Test_print ();

Getchar ();

}

After debugging, you can see that the call is successful.

 

Ii. creation and use of dynamic libraries 1. Static Loading of dynamic libraries

1) create a project static_load

Static_dll.h

// Export class

Class _ declspec (dllexport) static_test

{

Public:

Void print ();

};

// Export the Function

_ Declspec (dllexport) void static_function ();

// Export the object

Extern _ declspec (dllexport) static_test static_ObjTest;

Static_dll.cpp

# Include "static_dll.h"

# Include <stdio. h>

Void static_test: print ()

{

Printf ("static_test: print () in static load dll. \ n ");

}

Void static_function ()

{

Printf ("static_function in static load dll. \ n ");

}

Static_test static_ObjTest;

 

 

_ Declspec (dllexport) indicates the export item of the dynamic library. Otherwise, the variables, functions, and classes cannot be found. Select the configuration type: dynamic library (dll ).

 

2) use the dynamic library for Static Loading

First create a dll. h

// Import class

Class _ declspec (dllimport) static_test

{

Public:

Void print ();

};

// Import Functions

_ Declspec (dllimport) void static_function ();

// Import object

Extern _ declspec (dllimport) static_test static_ObjTest;

_ Declspec (dllimport) indicates that this item is an external import item imported from the dynamic library.

Create a test project uselib

Main. cpp

# Include "dll. h"

# Include <stdio. h>

Int main ()

{

Static_test st;

St. print ();

Static_function ();

Static_ObjTest.print ();

Getchar ();

}

At the same time, you must put static_load.lib and static_load.dll in the same directory and import the. lib file to the resource file.

 

2. dynamically load the dynamic library

1. Set up the project dynamic_load for the difference

Dynamic_dll.h

// Export class

Class _ declspec (dllexport) dynamic_test

{

Public:

Void print ();

};

// Export the Function

_ Declspec (dllexport) void dynamic_function ();

// Export the object

Extern _ declspec (dllexport) dynamic_test dynamic_ObjTest;

Dynamic_dll.cpp

# Include "dynamic_dll.h"

# Include <stdio. h>

Void dynamic_test: print ()

{

Printf ("dynamic_test: print () in dynamic load dll .");

}

Void static_function ()

{

Printf ("dynamic_function in dynamic load dll .");

}

Dynamic_test dynamic_ObjTest;

Select the configuration type: dynamic library (dll). then compile (build ).

 

Create a project uselib

# Include <stdio. h>

# Include <Windows. h>

Int main ()

{

HINSTANCE hDLL = LoadLibraryA ("dynamic_load.dll ");

If (hDLL = NULL)

{

FreeLibrary (hDLL );

Return 0;

}

Typedef void (_ stdcall * func )();

Func f;

F = (func) GetProcAddress (hDLL, "dynamic_function ");

If (f) (* f )();

Getchar ();

}

Note that the exact dll path must be input in LoadLibraryA. If the char * path is used, LoadLibraryA is used; otherwise, LoadLibraryW is used for unicode. The dynamic import class is a little troublesome, so we will not discuss it here.

There is a good piece of information: http://www.cnblogs.com/skynet/p/3372855.html

Classes in dynamic loading dll: http://www.cppblog.com/codejie/archive/2009/09/24/97141.html


Do I have header files when creating a static or dynamic library? Why?

Not necessarily required. Creating a database is generally for the following two purposes:
1. package related code into a library and publish it to other users.
This is the most common case, for example, libgcc is used for writing C language. In this case, apart from providing the library file: static library [in windows. lib, linux. a]; dynamic library: [in Windows. dll in Linux. header files must be provided. The header file is the interface provided in your library for external use. If no header file exists, others cannot use it.

2. Write plug-ins for some software.
Many large projects are modular and have some specific interfaces for customization. When the program runs, it dynamically loads the dynamic library under the specified directory and calls the methods agreed in the dynamic library during the runtime.
In this case, the header file is not required, but the library must be implemented according to specific conventions.

This is probably the case above.

To achieve the following functions, how can we design a library (combining dynamic and static libraries )?

The static Connection Library provides complete target code for functions. If a program calls a function in the static Connection Library, during the connection, the connection program copies the code of the function contained in the static Connection Library to the running file.
Instance:

The dynamic connection library is an executable module. Its functions can be called by Windows applications to execute some functions. The dynamic Connection Library provides services for application modules. The USER. EXE, KENERL. EXE, and GDI. EXE modules in the Windows Kernel are both dynamic connection libraries that provide services such as USER message service, process management, and graphic output.
Instance:

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.