It is a bit complex to know how to install CppUnit and how to use it with Eclipse.
That’s why I make this tutorial about CppUnit and Eclipse.
I hope it will be easy enough even for beginners, if not, do not hesitate to post a comment, I will help if I can.
This installation will be done on Windows OS, I am sure if you are a Linux addict or a Mac fan, this explanation will help you as well.
1. Installation of CppUnit with Eclipse on Windows
A. The CppUnit framework
Of course, we need the source of the CppUnit framework.
No problem there, take it up from the official host website:
http://sourceforge.net/projects/cppunit/files/cppunit/
So extract all the files on the directory of your choice, for example, I will put them in this one:
C:\soft\libraries\cppunit-1.12.1
We need now to configure the framework.
At this point, you need at least a GNU tool for Windows, such as MinGW. If you don’t have it, check on my website, and follow instructions.
If you installed MinGW without Make, you should also need the MSys, a brother of MinGW.
MSys contains the GNU Make tool.
So, open MinGW and go until the directory where you put CppUnit, in our case:
C:\soft\libraries\cppunit-1.12.1
Execute the following command:
./configure
Once it done, execute this second one:
make
To finish execute this last one:
make install
Don’t be afraid, it may take at least 10 minutes for the 3 commands to be performed.
OK, if you don’t have any error, CppUnit is now configured for your system.
That’s a great step.
B. CppUnit with Eclipse
Open Eclipse then create a new C++ Project.
The Project name is MyCppUnitStaticLibrary, its type is an empty Static Library, and the Toolchain is MinGW GCC.
For example File > New > C++ Project > Static Library > Empty Project > MinGW GCC > Finish.
Really basic.
Open this project and create a new folder inside, such as myimports.
Right click this new folder and select Import > General > File System > Next.
In the From directory input, click Browse and enter the directory where you put the CppUnit source, in our case: C:\soft\libraries\cppunit-1.12.1\src\cppunit.
Click Select All > Finish.
It will import all files from src/cppunit into myimports.
OK, let’s continue by right clicking the project then > Properties > C/C++ Build > Settings >Tool Settings > GCC C++ Compiler > Includes.
Click Add… on the right (a tiny icon with a green plus) to add a directory path.
In our case it will be: C:\soft\libraries\cppunit-1.12.1\include.
Click OK > OK.
Our project library is now ready to be built.
So let’s do it.
Right click the project then > Build Project.
A new lib has been created in the Debug folder: libMyCppUnitStaticLibrary.a.
Let’s continue.
Create a new C++ Project named MyNewProject.
File > New > C++ Project > Executable > Empty Project > MinGW GCC > Finish.
Right click MyNewProject > Properties > C++ Build > Settings > Tool Settings > GCC C++ Compiler > Includes.
On the right, click the tiny green icon on the Include paths (-I) panel and add this:
C:\soft\libraries\cppunit-1.12.1\include.
Click OK.
Let’s continue.
MyNewProject > Properties > C++ Build > Settings > Tool Settings > MinGW C++ Linker > Libraries.
On the right there are two panes.
On the first, at top, Libraries (-l), click the green plus icon and add this: MyCppUnitStaticLibrary.
Indeed, in C++ we must not use the prefix lib and the .a extension.
The main reason is that the linker doesn’t accept it.
In the second pane, at bottom, Library search path (-L), let’s enter in which directory this lib is.
So let’s add it, in my case it is: C:\dev\cpp\MyCppUnitStaticLibrary\Debug.
Click OK.
Here we are, let’s test it!
In the cppunit sources that you have installed, there is a folder named examples:
C:\soft\libraries\cppunit-1.12.1\examples.
Open it and copy paste the money folder into your MyNewProject.
Build this project (rigth click > Build Project).
Once it done, right click again this project > Run As > Local C/C++ Application.
In the console we can see four full stops (….) with an OK and (4).
Just like that:
....
OK (4)
It means that your installation of CppUnit with Eclipse on Windows is completed!
You are now ready to use it.
Well done you’ve made it!