Static and Dynamic Link Libraries in Windows and Linux systems (1)

Source: Internet
Author: User
Tags visual studio 2010

Recently, I suddenly wanted to write a cross-platform project and found that the first cross-platform library to be called was solved. So I studied the problem of libraries on two platforms. This article describes how to create a static link library and a dynamic link library in Windows and Linux operating systems.
1. Introduction
1.1 why use a library (static and dynamic link libraries )?

Software design generally classifies functions to improve code reusability, maintainability, and testability. Libraries are the embodiment of modular development. They encapsulate code with classification functions for other modules to call.
1.2 What are static and dynamic link libraries?

The concept cannot be strictly described, but the idea is basically the same in both Windows and Linux systems. If a function in the static Link Library is called in the program, the code of the called function (the compiled Binary Image Code instead of the source code) it needs to be embedded into the program for compilation together, forming a whole with the program. Obviously, the size of the program file that calls the static Link Library is larger. In Windows, the suffix is. Lib. in Linux, the suffix is.. If the function in the dynamic link library is called in the program, the dynamic link library only needs to save one copy in the memory for all the programs to call. In Windows, the suffix is. dll. in Linux, the suffix is in the format of. So. x. x. When discussing DLL files in windows, this article only mentions one non-mfc dll, that is, non-MFC dynamic library, and does not involve the other two (MFC regular DLL and MFC extension DLL)
Generally, the dynamic link library is more widely used, so the focus is on the dynamic link library.

2. Static and Dynamic Link Libraries in Windows

2.1 static Link Library in Windows
Create a simple static Link Library and use the C language. The development tool is Visual Studio 2010 and the operating system is Windows 7.
(1) Create an empty project named teststaticlibproject, and change the property marked in red in the project properties, such:

 

(2) Create the header file static_lib.h and the source file static_lib.c. The Code is as follows:

# Ifndef _ static_lib_h <br/> # DEFINE _ static_lib_h </P> <p>/* function declaration for import or export */<br/> void test_static_lib_method (void ); </P> <p> # endif

/* Functions provided by the static Link Library */<br/> # include <stdio. h> </P> <p>/* use this macro definition to declare the function of the header file as an export function */<br/> # include "static_lib.h" </P> <p> void test_static_lib_method (void) <br/> {<br/> printf ("test static library method. /n "); <br/>}< br/>

(3) run the command to obtain teststaticlibproject. Lib, which is the static Link Library.

 

 

 

2.2 dynamic link library in Windows
To create a simple dynamic link library, follow these steps:

(1) Create an empty project named testdynamiclibproject and change its output in the project, for example:

Create the header file dynamic_lib.h and the source file dynamic_lib.c. The Code is as follows:
/* Dynamic_lib.h */</P> <p> /*************************** **************************************** * ********** <br/> use this header file in two projects: <br/> (1) Dynamic Link Library Project (that is, this project) <br/> (2) project that references the dynamic link library <br/> ***************************** **************************************** * ********/<br/> # ifndef _ dynamic_lib_h <br/> # DEFINE _ dynamic_lib_h </P> <p> /****** ** solution 1: use the _ declspec keyword to export the function ********************************** ****************** <br/> // import or export the function prefix in the dynamic link library <br/> # ifdef dynamic_dll <br/> # ifdef _ cplusplus <br/> # define extern "C" _ declspec (dllexport) <br/> # else <br/> # define extern _ declspec (dllexport) <br/> # endif <br/> # else <br/> # define extern _ declspec (dllimport) <br/> # endif </P> <p> // function declaration for import or export <br/> extern void test_dynamic_lib_method (void ); <br/> ************************************ **************************************** * *******************/</P> <p>/********** solution 2: use. ************************************ * ***********************/<br/> void test_dynamic_lib_method (void ); <br/> /*********************************** **************************************** * ********************/<br/> # endif <br/>

/* Dynamic_lib.c */<br/>/* functions provided by the dynamic link library */<br/> # include <stdio. h> </P> <p>/* use this macro definition to declare the function of the header file as an export function */<br/> // # define dynamic_dll <br/> # include "dynamic_lib.h" </P> <p> void test_dynamic_lib_method (void) <br/> {<br/> printf ("test dynamic library method. /n "); </P> <p >}</P> <p>

(2) There are two ways to export a function from a DLL:

1. Define extern _ declspec (dllexport) in the export function. For details, see solution 1 of header file and source code. By the way, if C ++ needs to export the class, a similar format is used in the class declaration in the header file, for example, class _ declspec (dllexport) Point.

2. Create a module definition file (. Def) and use the. Def file when creating a DLL.

A brief description of how to create a. Def File
(1) The first line must use the keyword library to describe the DLL name. For example:
Library libcstl
(2) The second line can be the exports keyword. At first glance, you will know that you want to export the function. Exports is followed by the "function name" and "Number of the exported function" (which consists of the "@" symbol and number ), if the number of the exported function is omitted, the compiler automatically generates the number. The following is an example of how to export three functions:
_ Type_register @ 1000
_ Type_duplicate @ 1001
_ Type_debug @ 1002

(3) Others: You can use the Semicolon ";" to complete the annotation. You can specify the version number for the version keyword, and the description statement can describe the usage of the DLL.

(3) run the command to obtain testdynamiclibproject. dll and testdynamiclibproject. Lib, which are dynamic link libraries.

 

These two export methods have their own purposes in some cases. For example, the first method is suitable for self-calling in VC projects, and the second method is suitable for cross-platform or cross-language use.

 

2.3 Test Items

The above two linked libraries are created and can be used in the project. Create an empty project named testproject (empty project) and reference the above two libraries. The steps are as follows:

(1) Reference static library: enter the name of the static library in the input box.

(2) reference the dynamic library. In addition to the Lib name (which contains the identifier) corresponding to the dynamic library in the figure above, you also need to fill in the directory of the Dynamic Link Library as shown in:

 

 

(3) The project code for calling the library is as follows, named main. C:

# Include <stdio. h> <br/> # include <stdarg. h> </P> <p> # include "local_method.h" <br/> # include ".. /testdynamiclibproject/dynamic_lib.h "<br/> # include ".. /teststaticlibproject/static_lib.h "</P> <p> int main () <br/>{</P> <p> printf (" Hello, world. /n "); </P> <p>/* (1) Test the local Method */<br/> test_local_method (); </P> <p>/* (2) Test the dynamic link library Method */<br/> test_dynamic_lib_method (); </P> <p>/* (3) method for Testing static state Link Library */<br/> test_static_lib_method (); </P> <p> getchar (); </P> <p> return 0;

 

Download the source code of this project. For more information about Linux, write it next time.

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.