C language writing static link library and its use

Source: Internet
Author: User
Tags prototype definition what header win32

This article describes the use of C language to write a static link library, and the use of C and C + + methods to invoke and so on.

A static library program: The runtime does not exist independently, linked to the executable file or dynamic library, the target program archive.

1. Writing static library steps with C

A. Establishment of the project (Win32 Static Library)

B. Add library program, source file using C file (Win32 Static Library)

CLIB.C Library Source files

<pre name= "code" class= "CPP" >int clib_add (int add1,int add2) {return add1+add2;} int clib_sub (int add1,int add2) {return add1-add2;}

Compile, link after generate Clib.lib, use this file later

2, the use of static library

A. Create a C file, you can directly use the C library function in the file, do not need a header file, C compiler just according to the library function name, in the library to find the corresponding function code, to link.

B. The path setting of the library, set the library path in the project's "Setting", or you can use the #pragma keyword to set the #pragma comment (lib, ". \\clib\\clib.lib ")

3, c the way to call the C language written by the static library

C Type

C Compiler what header file and function declaration do not ... #pragma comment (lib, "... \\clib\\clib.lib ")//notify the linker to find the source code. int main (void) {int num1 = 100;int num2 = 1000;int NSum = Clib_add (num1,num2); int nsub = Clib_sub (num1,num2);p rintf ("nSum =%d,nsub =%d\r\n", nsum,nsub); return 0;}
</pre><pre name= "code" class= "CPP" >4, <strong>c++ way to invoke the C language of the static library written </strong>

In the CPP environment use C Static Library, the function prototype definition in the library to add extern "C", for example: extern "C" int Add (...);

C + + functions must declare that this is a function feature of C + +//c function does not have to declare, this is the characteristic of C//c++ compiler compiler function, must find the declaration, in that way to compile the function. extern "C" int clib_add (int num1,int num2), extern "C" int clib_sub (int num1,int num2),//extern "C" to C + + compiler, this is compiled in C language format .//The above two sentences are for the compiler to see. #pragma comment (lib, "... \\clib\\clib.lib ")//The above code is for the linker to see. int main (void) {int num1 = 100;int num2 = 1000;nsum  = Clib_add (num1,num2); nsub  = Clib_sub (num1,num2);cout<< "nSum =" <<nSum<< "nsub =" <<nsub<<endl;return 0;}


C language writing static link library and its use

Related Article

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.