Codeblocks creating and using a static library (C language)

Source: Internet
Author: User

A static library (with a. A or. lib extension) is a file that contains functions that are used to consolidate the execution of the program during the link phase, and that the dynamic link library (extension. dll) is not integrated into the execution program in the link phase.

DLL files are dynamically invoked during the execution phase

Below we will develop the static library with the free development tool Codebocks

Create a static library

Start Codeblocks and create a project of type "Static Library" (file/new/project/static library/go).

Give the project a name ("Malibrairie" for instance) and assign a target directory ("C:/essai" for instance)

Delete the default content in the Main.cpp file. Enter into the program by following the contents below.

void cinq (int *i)
{
int n;
N=*i;n=5*n;
*i=n;
}

Select the "Build/run" menu.

Codeblocks immediately generates the library file to the "C:/essai/malibrairie" directory and Names "Libmalibrairie a".

Codeblocks and Devcpp build the library file with the extension ". A", Visual Studio will build the library file with the extension ". Lib".

We use this library file below.

The library file "Libmalibrairie.a" has been created.

Use the Static library

Create a new project with the type "Win32 GUI".

Select the application type "Frame based".

Name ("Monprogramme") and select the project directory ("C:/essai").

Remove the main.cpp default code and replace the code with the following code to test our static library.

#include "Windows.h"
extern "C" void cinq (int *);
int Apientry WinMain (hinstance h1,hinstance h2,lpstr l,int N)
{
int x;
Char texte[80];
x=2;
Cinq (&x);
wsprintf (Texte, "%d", x);
MessageBox (Null,texte, "", MB_OK);
return 0;
}

Usually, Codeblocks does not know that the library file is being used and its location, we must declare the library file so that it does not go wrong when linking. Select the menu "Project/build options/linker Settings" To add the "Libmalibrairie.a" library file. (For personal testing, you need to add a directory of library files to the linker search directory).

Click "Build/build and Run". The program runs correctly.

When using the-lapue library in Apue, write the apue directly. Because the static library file has been added to the/usr/lib/libapue.a.

Codeblocks creating and using a static library (C language)

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.