Tuesday, April 17, 2012

Correctly configuring googlemock on Mac with eclipse

So, if you ever do C++ development on a mac, you'll want a unit testing framework.

The GoogleMock utility, available at http://code.google.com/p/googlemock/, includes both a unit testing framework and a system for creating mock objects. 

cppcheck and splint are both static source checkers, and they help you find errors in your C++ code. It's really cool to have a problem, not know how to fix it, discover that it is actually really vague and that it might take hours to find the source and solve it-only to be able to run a single command that will give you a strong indication or at least a hint of where to look to find the solution.

All, log4cxx is kind of a debugging tool with a lot more features than the usual printf statement. Enhance your execution having the ability to record where execution went and what every variable was when it ran! I haven't used log4cxx, but I know that if you use log4j, you could just add a line that would set the logging sensitivity, and if you set it to pedantic or something it would log the entrance into every function and every variable change.

I just spent a day or so configuring googlemock for a computer networks class project. I can say that if you don't read the instructions carefully, it will really bite you. 

If you are on a linux system, you can install of these with a single command:
sudo apt-get install google-mock cppcheck splint liblog4cxx10

But I've been doing all the work on mac, so I'll give step by step instructions on mac. After untarring: 
cd gmock-1.6.0 ; autoreconf -fvi ; GMOCK_DIR=/path/to/gmock-1.6.0 ; GTEST_DIR=$GMOCK_DIR/gtest ; 
g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -I${GMOCK_DIR}/include -I${GMOCK_DIR} -c ${GTEST_DIR}/src/gtest-all.cc ; 
g++ -I${GTEST_DIR}/include -I${GTEST_DIR} -I${GMOCK_DIR}/include -I${GMOCK_DIR} -c ${GMOCK_DIR}/src/gmock-all.cc ; 
ar -rv libgmock.a gtest-all.o gmock-all.o

then move libgmock.a to $GMOCK_DIR/lib

cd $GMOCK_DIR/make ; make ; ./gmock_test && echo "all tests should succeed!"

#NOTE: you might have to run the configure command in $GMOCK_DIR somewhere along the line... I did it, but it's not in the instructions ; cd $GMOCK_DIR ; ./configure 

If you want eclipse to work with this, add a new build configuration and add the include files from gmock and gtest, then add the archive in the link step. To do all of these, in eclipse, go to project -> properties -> C/C++ General -> Paths and symbols -> includes
(make sure that you have the right build configuration selected, the ones that you will build and run your tests from.)
select GNU C++ under languages, and then on the right where it says add, click the button, then click filesystem. Add $GMOCK_DIR/include and $GTEST_DIR/include (not the variables, I mean actually navigate to those directories!)

Now that you've done that, to include the archive file in the link step:
Project -> properties -> C/C++ Build -> Settings -> MacOSX C++ Linker
In the command line pattern textbox, add the directory to your libgmock.a that you compiled, should be at $GMOCK_DIR/lib/libgmock.a

The above steps work on all platforms, but doing a manual setup is a pain, especially a nuisance because in linux you want everything to be managed by the repo. Because I did my setup on mac, I don't know where the libgmock.a file is, and I don't know if it automatically places the gmock include files in the default include path where it would just get found by the compiler if you just did #include <gmock/gmock.h>... You have to do your own exploring here, but it shouldn't be too hard, as the step are 1-1 from what I've done. Just find out how it sets up the include and if it drops a libgmock.a file anywhere, or if you have to compile that.

No comments:

Post a Comment