A deep analysis of C + + program Library

Source: Internet
Author: User

Andrew Hunt/david Thomas Programmer's Way of practicing

In computer science, a library is a collection of subroutines used to develop software. The difference between libraries and executables is that libraries are not stand-alone programs, they are code that provides services to other programs.

A library is a written, existing, mature, reusable code. In reality, every program relies on a lot of underlying libraries, and it's impossible for everyone's code to start from scratch, so the library's existence is very interesting.

Dynamic libraries and static libraries

A library link refers to one or more libraries included in the program, there are two types of links: static links and dynamic links, the former linked to the library is called a static library, the latter is called Dynamic Library.

Dynamic Library and static library compilation process

Static links

Static linking is the way the linker adds the contents of a library to an executable program when it is linked.

When a program is connected to a static library, the object file in the library contains all the machine code for the function that will be used by the program to be copied into the final executable file. This results in a relatively higher amount of executable code being generated and more disk space and memory footprint.

In Windows, a static library is a file with a. lib suffix, and in Linux the static library is a file with a. a suffix.

Dynamic Link

Dynamic links that are loaded by the operating system loader when the executable file is loaded or run.

The executable file connected to the shared library contains only the reference table for the function it needs, not all the function code, and only the required function code is copied into memory when the program executes. This makes the executable file smaller and saves disk space. However, because the runtime to go to the link library will take a certain amount of time, execution speed is relatively slow.

In Windows, dynamic link library is a file with a. dll suffix, and in Linux, a dynamic library (shared library) is a file with the. so suffix.

Comparison between dynamic and static libraries

The biggest disadvantage of static linking is that the resulting executable file is too large, and another problem is that static libraries can cause problems with updates, deployments, and publishing pages for programs. If the static library is updated, the application that uses it will need to be recompiled and published to the user (for the player, it may be a minor change that will cause the entire program to be downloaded again, full update).

The biggest disadvantage of dynamic linking is that the executable program relies on the separately stored library files to perform correctly. If the library file is deleted, moved, renamed, or replaced with an incompatible version, the executable program may not work properly.

Dynamic libraries are not connected to the target code when the program is compiled, but are not loaded until the program is run. If different applications call the same library, then only one instance of the shared library is needed in memory to avoid the problem of space wasting. Dynamic libraries are not loaded until the program is run, but they also solve the problem of the static library's updating, deploying, and publishing pages for the program. The application only needs to update the dynamic library to implement the program update.

Using VC + + to create dynamic libraries under windows

There are two ways to create a dynamic library under Windows: Using the _declspec (dllexport) and _declspec (dllimport) keywords, specifying a. def file.

Declaration _declspec (dllexport) and _declspec (dllimport) keywords
The Declaration function is _declspec (dllexport), which indicates that the function is exported to a DLL, and declaring a function as _declspec (dllimport) indicates that the function is exported from a DLL.

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
__declspec (dllexport) void Test ()
{
printf ("DLL test\n");
}
#ifdef __cplusplus
}
#endif

By using Exescope to load the DLL, you can see the following information in the export table:

Sequence address name
00000001 10011127 Test

Specify a. def file

. DEF specifies the function and tells the compiler not to use the decorated function name as the exported function name, but to export the function with the specified function name.

1, need to create a module-definition file (. def) files, add the name of the export function


Exports
Test

Note: If you change the. txt file to a. def file, you need to set it in Visual Studio: Project Property Pages→configuration Properties→linker→input→module Add this file in Definition file

2. Add test function code

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
void Test ()
{
printf ("DLL test\n");
}
#ifdef __cplusplus
}
#endif

By using Exescope to load the DLL, you can see the following information in the export table:

Sequence address name
00000001 10011127 Test


If you add only a. def file without adding the exported function name, the exported function table exists, but the function table is empty.

How to choose?
If you export function calls by using CDECL, you can use a. def file if you want to use the StdCall invocation method and do not want to be decorated with a letter name. The explanation of the calling mode is shown in the following article.

How to invoke DLL dynamic link library

There are two ways of calling under Windows platforms: explicit invocation and implicit invocation. Use the following example to illustrate this issue:


Test.h
#ifdef __cplusplus
extern "C" {
#endif
__declspec (dllexport) void Test ();
#ifdef __cplusplus
}
#endif
Test.cpp
#include "test.h"
#include <stdio.h>
__declspec (dllexport) void Test ()
{
printf ("DLL test\n");
}

Explicit invocation of

Explicit calls are loaded into the dynamic-link library through LoadLibrary, and the GetProcAddress function is used to obtain the exported function address.


#include <windows.h>
int main ()
{
typedef void (*TESTFUNC) (void);
TestFunc ptestfunc = NULL;
HINSTANCE hinstance =:: LoadLibrary (L "Dlltest.dll");
if (!hinstance)
{
return-1;
}

Ptestfunc = (testfunc) GetProcAddress (hinstance, "test");
if (Ptestfunc)
{
Ptestfunc ();
}
return 0;
}

implicit invocation of

Implicit invocation through the #pragma comment (lib, "Xx.lib") way, will xx.lib this directly into the project to link, and then through the #include related header file can directly use the exported function.

1. Set Include directories and library directories

If you use Visual Studio, you set both options in the Project property pages→configuration properties→vc++ directories, which are used to include the "test.h" directory where the directory and "Test.lib" reside.

2. Set Library dependencies

If you are using Visual Studio, in Project property pages→configuration properties→linker→input→additional dependencies, add the Test.lib "Library. or direct use of Pragma:

#pragma comment (lib, "Test.lib")

3, the calling code is as follows:


#include "test.h"
#pragma comment (lib, "Test.lib")
int main ()
{
Test ();
return 0;
}

Some concepts about dynamic libraries

extern C and Name-mangling
The Name mangling is a protocol for standardizing the notation table notation for communication between compilers and linker, which aims to make the symbol have enough semantic information to ensure that the link process is accurate according to the language specification of the program.

However, the C + + standard does not prescribe name-mangling scenarios, which leads to different compilers using different scenarios, and the compiled obj file is not generic. C Standard stipulates the specification of C language name-mangling, any one support C language compiler, the compiled obj file can be shared. Let's look at the results of C and C + + compilation:


#include <stdio.h>
__declspec (dllexport) void Test ()
{
printf ("DLL test\n");
}
We use C + + compiler and C compiler to compile this piece of code, use Exescope to load the DLL, view the export table can see the following information:

1, C + +:

Sequence address name
00000001 10011078? test@yaxxxz
2, C

Sequence address name
00000001 10011127 Test

for different C + + compilers, the resulting function export names are probably not the same, and you may experience problems when you display calls to these exported functions, and C compilation does not.

Using extern c to declare the parcel code, you can compile the code in the C + + compiler in the form of a compiler.

#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
__declspec (dllexport) void Test ()
{
printf ("DLL test\n");
}
#ifdef __cplusplus
}
#endif
The result of this code being compiled in the C compiler and C + + compiler is the C-style export function.

Sequence address name
00000001 10011127 Test

calling convention

C and C + + is the default invocation of CDECL, in the export function, the use of cdecl and __stdcall invocation method, the exported function name is also different. See the following example:


#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
__declspec (dllexport) void __cdecl/__stdcall test ()
{
printf ("DLL test\n");
}
#ifdef __cplusplus
}
#endif
We declare the calling convention of the test function as cdecl and stdcall respectively, and the result is as follows:

1, __cdecl

Sequence address name
00000001 10011127 Test
2, __stdcall

Sequence address name
00000001 10011097 _test@0
It is recommended that you use the __cdecl invocation method so that you only need getprocess (HINSTANCE, "test") when you explicitly call the exported function.

Why do I need to encapsulate dynamic libraries in C language?
In our normal development of dynamic library, even if our source code is C + + language, but usually in the export will be encapsulated into the C language style, this is why?

DLL is the corresponding C language dynamic link technology, if our library only for C + + language use, will not involve multi-language calls, then use C + + package Dynamic library is also possible. But if our library needs to call VB, C # and other languages, the dynamic library of the C + + language package needs to be used in a variety of ways, and the dynamic library in C language only needs to display the call.

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.