C language writing static link library and its use

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

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 execution is not independent, 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, the ability to directly use the C library function in the file, no header file, C compiler just according to the library function name, in the library to find the corresponding function code, to link.

B. Path settings for the library. The library path is set in the "Setting" of the project, and can also be set using the #pragmakeyword #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 ")//Notification linker. Find the source. 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>

Use C static libraries in the CPP environment. In the library function prototype definition to add extern "C", for example: extern "C" int add (...);

C + + functions must declare that this is a function feature of C + +//c functions do not have to be declared. This is a feature of C//c++ compiler compiler function, you 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 according to 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.