Talking about how C + + calls static link library under Windows platform

Source: Internet
Author: User

Talking about how C + + calls a static link library under Windows platform 1. What is a static link library? Why use a static link library?

The explanation for static libraries on Wikipedia is this: in Computer science, static libraries (English: The Static Library, statically-linked Library), or static libraries, are a collection of external functions and variables . The file content of a static library usually contains a bunch of programmer-defined variables and functions that are not as complex as a dynamic-link library, and are integrated into the application by the compiler and the connector during compilation , and produced as target files and executable files that can be run independently.

As can be seen clearly in the above explanation, the concept of static libraries consists of two parts. One is "library", the so-called library, is actually some programmer's custom functions or variables (for C + + may have custom classes), these functions or variables can accomplish certain functions, and therefore be assembled together into a library, to achieve the purpose of code reuse. Of course you might ask, why do you give these functions or variables in the form of a library? Isn't it better to give the source code directly? Indeed, if you have source code, we can add the corresponding. cpp or. h files directly to the project, and you do not need to use a static link library. However, in many cases we are not getting the source code (the source code may be a trade secret), or the provider of the code is reluctant to let us modify the code inside, the use of the library is the best choice, the second part of the concept is "static", so-called static, and dynamic corresponding. The so-called static, is that during compilation by the compiler and the connector to integrate it into the application, in short, this part of the code is already in the executable file (the Windows system is an. exe file), directly double-click to run, no longer need other files. Dynamic link library is different, the dynamic link library related code is not integrated into the application, in the Windows System Dynamic link library is a. dll file, if only. exe file and missing. dll file, the application will be prompted not to find the DLL, does not run correctly.

The advantages and disadvantages of the static link library can be drawn from the above instructions:

    • Advantage: Publish only. exe files when publishing, library code has been integrated into the executable file, regardless of whether the client computer installed the appropriate library;
    • Disadvantage: The executable file size is large, if more than one executable file references the same library, the library must be integrated into each executable file, not conducive to reuse.
2. How do I write a static link library? (Take Visual Studio under Windows platform as an example)

We use Visual Studio under the Windows platform as an example to write a static-link library in the C + + language and then invoke it in another project.

2.1 Writing a static-link library using Visual Studio

First open vs, New project, project type selected as "Visual C + + console Application".

Then select the static library at the application type of the wizard, which is the statically linked library and click Finish.


Click Finish after the project is established, and then to the project to add a source file and a header file, the source file named Test.cpp, the header file named Test.h. Where Test.cpp is the implementation of the function, Test.h is the declaration of the function.

We write a declaration of the example function in Test.h, which takes an int variable and returns the value of the variable as the return value.

Add the following code to Test.h:

//这是一段示例代码#pragma onceexternint ReturnAInt(int val);

This code declares a function that takes an int variable and returns an int value. Unlike the general function declaration, which has an extern keyword in front of it, the extern keyword indicates that it is an external function that is not implemented by itself, but is implemented by an external library, so that the linker can link it.

Then add the implementation code for the function in Test.cpp:

#include"stdafx.h"int ReturnAInt(intval){    val;}

Build this project, you can see the corresponding. lib file in the project directory, this is the generated static link library file.

2.2 Referencing a static link library in a project

Here we create a new project to reference the static link library written above.

After the project is built, copy the Test.h header file you just wrote to the project directory.

Add the header file in the project, choose to add the existing header file, Test.h this file to the project, and then add the source file Main.cpp in the project, adding the following code:

#include "Test.h"#include <iostream>int main(){    std::cout << ReturnAInt(15std::endl;    getchar();}

Running the code, we will find that the VS hints the following error:

This is a link error that indicates that the Returnaint external symbol cannot be parsed, stating that although we provide a declaration of this function, VS cannot find the implementation of this function because we do not specify the location of the. lib file, vs nature cannot find it. There are two ways to resolve this error:

Method one is to add the following code to the Main.cpp:

#pragma comment (lib,"..//Debug//CppLib.lib")

This line of code indicates the location of the. lib file, where the part of the double quotation marks is replaced by the actual path to the file, so that the VS is able to find the specified static link library based on this path, and then link it.

Method Two is to specify the location of the. lib file in the VS project settings. Right-click the project, select Properties, then select the VC + + Directories tab on the left, edit the library directories option on the right, and add the directory where the. lib file is located.

After clicking OK, right-click the references in the project and add the. lib file to the reference.

Click OK.

Either of the above two methods can be selected, and then run the project, the error is gone, the program runs successfully.

^_^

Talking about how C + + calls static link library under Windows platform

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.