VC ++ link library (2) Static Link Library

Source: Internet
Author: User

Before explaining the dynamic link library DLL, let's first explain the static Link Library. The example of a static link library can help us quickly build the concept of "library.


Example of a static Link Library


First, we use VC ++ to create a static Link Library, use VC ++ to create a Win32 static library project, and create two files libtest. h and libtest. cpp. The Code is as follows:

// File libtest. h

# Ifndef lib_h # define lib_h extern "C" int add (int x, int y); // declare as an external function of C compilation and connection mode # endif

// File libtest. cpp

#include "LibTest.h"  int add(int x,int y)  {      return x + y;  }  

Compile the project directly and obtain a. Lib file under the DEBUG directory under the project directory. This file is a function library. How can we use it?

------------------------------------------------------------------------ Gorgeous split line --------------------------------------------------------------------------
 

Next, we will create a simple Win32 console application project test, which has only one test. cpp file. The Code is as follows:

// File test. cpp

# Include <stdio. h> # include ".. \ libtest. H "# pragma comment (Lib ,".. \ libtest \ debug \ libtest. lib ") // specify to connect int main (INT argc, char * argv []) {printf (" 2 + 3 = % d ", add (2, 3 ));
   return 0; }  

Note that # pragma comment (Lib ,".. \ debug \ libtest. lib ") indicates that this file is generated. the OBJ file and the libtest file generated just now. link lib together.
If you do not need to specify the # pragma comment, you can directly set it in VC ++, select Tools> Options> directories> library files menu or options, and enter the path of the file to be imported.

Run the program. The result is as follows:

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.