Create static library with code blocks and create dynamic library with Code::blocks(with pictures)

In this post we will see how to create static library with code::blocks and how to create dynamic library with Code::blocks. Static library and dynamic library are easy to create using Code::blocks. Here, we will also see how to link them to our console application.

Link :C++ what is static library and dynamic Library
 
Creating Static and Dynamic library with Code::blocks


Create static library with code::blocks

Open your code blocks and go to File->New->Project, you will see a new window, scroll down and choose Static Library as shown below.

*Note::The library will be created without the header’s file, how to include the header file is shown later.

creating static library

Enter the program name “static_library” and click “Next” and “Finish”. You will see a new file main.c.

Delete the three functions int SampleAddInt(int i1, int i2), void SampleFunction1() and int SampleFunction2(). Let’s add our own functions, copy and paste the two functions given below.

 float Square( float f1 )
{
return f1*f1 ;
}
 
float Divide( float f1 , float f2 )
{
return f1/f2 ;
}

Try running the program, pres Fn + F8. You will get a message “You must select a host application to run a library” and click Ok.

Now go to the directory where your main.c is present. In my case, the directory is “C:\My_program\static_library“, you will see your static library file “libstatic_library.a” there.



Linking static library to console application

Next, we will link the library to our console application. Create a new console application and name it “new“. Delete all the code and Copy and paste the code given below in your main.cpp.

#include <iostream>
using namespace std ;

extern “C” float Square( float f1 ) ;

extern “C” float Divide( float f1 , float f2 ) ;

int main( )
{
cout<< Square( 1.2 ) << endl ;
cout<< Divide ( 2.3 ,3.1415 ) << endl ;

cin.get() ;
return 0 ;
}

Go to Settings->Compiler and click on it, a new window will appear which is shown below.

Linking the static library

Select the linker settings and click on Add then browse to where your libstatic_library.a is present and click OK. Your program is now ready to run. Press Fn+F8 and there it is your console black screen.


Adding header’s file to Static library

Lets add a header file to our static_library program. Open up your notepad or notepad++ and copy and paste the following code below.

#ifndef __STATIC_LIBRARY__

#define __STATIC_LIBRARY__
extern “C” float Square( float f1 ) ;
 
extern “C” float Divide( float f1 , float f2 ) ;

#endif ///static_library

Save it as static_library.h in the directory where your main.c is present.

Go to code::blocks and select settings->Compiler->Search directories->Linker. Click Add and add the directory where your static_library.h is present.The screen shot is given below.

including the static header file in console program

Now go to your main.cpp and delete extern “C” float Square( float f1 ) ; and extern “C” float Divide( float f1 , float f2 ) ; and include the header’s file #include “../static_library/static_library.h” and that’s it. Now run your program, press F8+Fn.

*Note: You may be wondering why do we need to include “extern “c” ” while declaring functions name in the header file. I don’t know if you have realize or not, but if you look at the source code of the library file, it has an extension of .c which is actually a C language file, not C++ file. So,the file is compiled using the C compiler not the C++ compiler. The keyword extern “c” tells the C++ compiler while compiling that the function is taken from C compiled library, this help the compiler to call the correct library function.



Create Dynamic library with code::blocks

First of all let’s create a new project to make the .dll file. If you have opened your code blocks go to New->Project, you will see a new window and select Dynamic Link Library. The screen shot is given below.

Creating a dynamic library

In the next Window, give the project title as “Dynamic_library“, click Next and Finish. You will see two new folders ‘Source’ and ‘Headers’ created under the Dynamic_library in the left panel of the Code::blocks. Under Source folder, you will find “main.cpp“, remove all the code present in the main.cpp file except #include “main.h”. And paste the following code below.

float DLL_EXPORT Cube( float f )
{
return f*f*f ;
}

Next, go to main.h under the ‘Headers’ folder and replace this void DLL_EXPORT SomeFunction(const LPCSTR sometext); with this float DLL_EXPORT Cube( float f ) ;. Now run the program, press Fn+F8. You will get a message You must select host application to “run” a library … and click Ok.

Now go to the debug directory of the Dynamic_library program, the directory in my case is “C:\My_program\dynamic_library\bin\Debug“. Under this directory you will find three files “dynamic_library.dll“,”libdynamic_library.a” and “libdynamic_library.def“. The ‘.dll’ file is the dynamic library.

Linking the dynamic library file

The next step is linking the .dll file in our console application. First create a new console application and name it “Console_dynamic_testing“. Next, go to the directory Settings->Compiler->Linker Settings and add the “libdynamic__library.a” file. The screen shot is given below.

Linking the dynamic library

Next go to the directory “Settings->Compiler->Search directories->Linker” and add the directory of the “main.h” file, then click OK .The screen shot is given below.

Adding .h file to console program

Run the program once, press Fn+F8. After running the program close it. And replace all your code in main.cpp with the code given below.

#include <iostream>
#include “../dynamic_library/main.h”
 
using namespace std ;
 
int main( )
{
cout<< Cube( 99 ) << endl ;

cin.get( ) ;
return 0 ;
}

Before running the program, transfer the “dynamic_library.dll” to the debug directory of your console application, the directory is most probably “../Console_dynamic_testing/bin/Debug“. And that’s it, run your program, press Fn+F8. If everything goes well the dynamic library will link and you will see your console black screen.




Leave a Reply

Your email address will not be published. Required fields are marked *