VC + + dynamic link library (DLL) programming static link library

Source: Internet
Author: User
Tags printf

The explanation of the static link library is not the focus of this article, but the example of a static link library can quickly help us to establish the concept of "library" before we specifically explain the DLL.

Figure 1 Creating a static link library

As shown in Figure 1, the new Static library project named Libtest in vc++6.0 (click here to download this project attachment) and create a new lib.h and lib.cpp two files, lib.h and Lib.cpp source code as follows:

File: Lib.h

#ifndef Lib_h

#define Lib_h

extern "C" int Add (int x,int y); External functions declared as C compilation, connection mode

#endif

File: Lib.cpp

#include "Lib.h"

int add (int x,int y)

{

return x + y;

}

Compiling this project gives you a. lib file, which is a library of functions that provides the functionality of Add. Once the header file and the. lib file are submitted to the user, the user can use the Add function directly.

The C library functions in standard Turbo C2.0 (which we use for scanf, printf, memcpy, strcpy, etc.) come from this static library.

Let's take a look at how to use this library, a new Libcall project in the workspace where Libtest works. The Libcall project contains only one main.cpp file, which demonstrates the method of calling a static-link library with the following source code:

#include

#include ". Lib.h "

#pragma comment (lib, "...). DebuglibTest.lib ")//Specifies to be connected with a static library

int main (int argc, char* argv[])

{

printf ("2 + 3 =%d", add (2, 3));

Static link library call is so simple, maybe we use every day, but we do not understand the concept. In code #pragma comment (lib, "...). DebuglibTest.lib ") means that the. obj file generated by this file should be connected with libTest.lib.

If you do not #pragma comment specified, you can set up directly in VC + +, such as Figure 2, select Tools, Options, directories, library files menu or options, fill in the library file path. Figure 2 adds the red circle to the path of the LibTest.lib file that we added.

Figure 2 Setting the library file path in VC

The example of this static link library lets us understand at least what the library functions are and where they come from. We now have the following vague realizations:

(1) library is not a monster, the process of writing a library and writing a general program is not very different, but the library can not be implemented alone;

(2) The library provides something that can be invoked by another program, and other programs must call it in some way to indicate that it is to be invoked.

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.