Monday, June 13, 2011

CUnit test - Installation and sample Test

I tried installing CUnit with default prefix and settings in Ubuntu11.04, but I always got error during compilation. Major error was "Fatal error: CUnit/Basic.h: No such file or directory". After doing a bit of googling, I stumble upon to the following URL with the instructions given below.
http://ubuntuforums.org/archive/index.php/t-1727067.html

Step 1: Download CUnit package from Source Forge.
Step 2: Find where you saved the package on your system, and uncompress it:
tar xjf CUnit-2.1-2-src.tar.bz2

Step 3: Go into the CUnit-2.1.2 directory.
cd CUnit-2.1.2

Step 4: Run the following sequence of commands:
mkdir -p $HOME/local

./configure --prefix=$HOME/local
make clean
make
make install

Step 5: Goto the directory $HOME/local. Verify that you have the following sub-directories: doc, include, lib, and share. Each of these should have the products you seek.

Step 6. To build an application using CUnit, something like this is used:

gcc -Wall -I$HOME/local/include MyProg.cpp -L$HOME/local/lib -lcunit -o myprog

OR

g++ -Wall -I$HOME/local/include MyProg.cpp -L$HOME/local/lib -lcunit -o myprog

Note: In your source code, include CUnit header files such as:

#include "CUnit/CUnit.h";
#include "CUnit/Basic.h";

==How to run the program

Since this library is not part of the system lib path, we need to include it each time into system variables or pass them as flag (as we did in gcc/g++ command above).

lets include this path into system variable.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/local/lib

Now run the program

./myprog

==== Now with System wide installation and Testing====

After practicing above sequence and commands for compilation and execution, I tried the same logic with system wide installation which was giving me errors early on. In Ubuntu, CUnit's head files are located at /usr/local/include and its libs are available at /usr/lib. So the compilation command would be something like this.

gcc -Wall -I/usr/local/include MyProgram.cpp -L/usr/lib -lcunit -o myprogram

To run, simply try ./myprogram and it should work. in case if it doesn't, set the LD_LIBRARY_PATH as we did above.
first make sure /usr/lib is not part of the LD_LIBRARY_PATH by printing this variable by 'echo $LD_LIBRARY_PATH'
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib

==Mac OS X (10.6)
The same system-wide installation and execution instructions work for mac os x 10.6.

5 comments:

Unknown said...

when i run test. Netbean IDE show 'cannot find -lcunit'

I have added
Cunit/basic.h & Cunit/Cunit.h

Unknown said...

cannot find -lcunit

alvinchew said...

can i unzip and put cppunit, junit into local as well? If so, is there unexpected problem?

Unknown said...
This comment has been removed by the author.
Unknown said...

I have files with the name raj_test.h and raj_test.c inside the folder ~/raj_lib/master/tests/raj and CUnit is located at ~/raj_lib/CUnit-2.1-3

When I try to run following command
gcc -Wall -I$HOME/local/include raj_test.c -L$HOME/local/lib -lcunit -o raj_test

I get an error as follows:
gcc: error: raj_test.c: No such file or directory

Where should I run the above command? Should I be running the above command at ~/raj_lib/master/tests/raj ? Or where CUnit is located?

Also, since I have .h and .c files, how can I combine both to get required binary.

Regards,
Raj