Create a C function library under VC

Source: Internet
Author: User

Create a static library project named libtest in VC, and create two files, Lib. h and Lib. cpp.Source codeAs follows:

// File: Lib. h

# Ifndef lib_h
# Define lib_h
Extern "C" int add (int x, int y); // declared as an external function in C compilation and Connection Mode
# Endif

// File: Lib. cpp

# Include "Lib. H"
Int add (int x, int y)
{
Return x y;
}

Compile the project to obtain a. Lib file, which is a function library and provides the Add function. After the header file and the. Lib file are submitted to the user, the user can directly use the Add function.

The C library functions (scanf, printf, memcpy, strcpy) in the standard Turbo c2.0 come from this static library.

Next let's take a look at how to use this library and create a new libcall project in the workspace where the libtest project is located. The libcall project contains only one main. cpp file, which demonstrates the call method of the static Link Library, its sourceCodeAs follows:

# Include <stdio. h>
# Include ".. \ Lib. H"
# Pragma comment (Lib, ".. \ debug \ libtest. lib") // specify the link to the static library

Int main (INT argc, char * argv [])
{
Printf ("2 3 = % d", add (2, 3 ));
}

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.