Create static and Dynamic library with Code::blocks(with pictures).


Creating Static and Dynamic library with Code::blocks
Static and dynamic library are easy to create using Code::blocks.Here,we will discuss how to create and link them to our console application.
 

Static library


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 header’s file, how to include header’s file is shown later.

creating static library

Enter a 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 ;
}

Run the program,Fn + F8.You will get a message “You must select a host application to run a library” and click Ok.Now got to the directory where your main.c is present.In my case the directory is “C:\My_program\static_library” ,you will see your library file “libstatic_library.a“.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 windows 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 ready to run.Press Fn+F8 and there it is your console black screen.
 

Adding header’s file to Static library

 
Lets add header’s 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 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.Run your program,F8+Fn.
 
*Note:You may be wondering why do we need to include “extern “c” ” while declaring functions name in header’s 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 file not C++.So,the file is compiled using C compiler not C++ compiler.The keyword extern “c” tells the C++ compiler while compiling that the function is taken from C compiled library and thus the compiler calls the correct library function.


 




Dynamic library


 
Let’s create new project to make .dll file.If you have open 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 window of the code::blocks.Under Sources you will find “main.cpp” ,remove all the code of the main.cpp file except #include “main.h”.After this paste the following code below.
 

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

 
Next go to main.h under Headers and replace this void DLL_EXPORT SomeFunction(const LPCSTR sometext); with this float DLL_EXPORT Cube( float f ) ;.Run the program ,Fn+F8.You will get the 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 next step is using the .dll file in our console application.First create a new console application or if you want to use the existing one ,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 this directory “Settings->Compiler->Search directories->Linker” and add the directory of the “main.h” file,the click OK .The screen shot is given below.
 
Adding .h file to console program
 
Next let’s use the .dll file in our console application.Create a new console application or you can also use it in any of your existing program.Let’s say you are creating a new console application,name the program “Console_dynamic_testing“.Run the program once,Fn+F8 and replace all your code 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“.That’s it run your program,Fn+F8.If everything goes well you will see your console black screen.
 

*Note::The process of creating .dll file for a plugin system is little bit different.Here’s some link for more information on creating .dll file for plugin system http://www.cplusplus.com/articles/48TbqMoL/ and https://forum.thegamecreators.com/thread/167112.