Eclipse CDT and Google GTest

If you have read my previous post about adding the gTest framework to your C++ project, you are very likely to have spent quite some time scratching your head trying to figure out how to make it play nicely with Eclipse’s CDT, and its (let’s face it) not terribly well-developed concept of ‘Build Configurations’.


Especially coming from Java and the extremely intuitive integration in Eclipse with JUnit, CDT’s approach to unit testing leaves a lot to desire.


I will not go through the basic setup necessary to add gTest to your project: please read the Get Started intro on Google Code, or my previous post; it’s pretty trivial anyway.


However, there are a couple of corrections (or perhaps, CDT 7.0 has fixed some of the limitations that required the workarounds I mentioned there).


Add gtest.a to the Test Configuration that you define for your project
The best way to add the gTest libraries would be to create a symbolic link from /usr/local/lib to the /make/gtest.a library, created by the default install process)

$ sudo ln -s make/gtest.a /usr/local/lib/libgtest.a


and then add /usr/local/lib to the Linker’s library search path (-L/usr/local/lib) and libgtest.a to the list of included libraries (-lgtest) using Project > Properties:





Add the includes from gTest to ALL build configurations

Do this by adding the /include/ folder to your project’s include search path (Project > Properties);







Now, to make the unit tests run, we need to add a fairly trivial main() method (taken from gTest’s Primer doc): 

#include "gtest/gtest.h"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}



along with the normal test fixture(s) in either one or multiple .cc files, whichever way works best for you.
(personally, I prefer the latter: one C++ class per .cc file, and the associated *_test.cc file alongside – keeps me sane!  This, obviously, requires that one just has one main() method in a separate source file, which will drive the entire test suite).


However, this will cause trouble when linking with a Debug/Release configuration, and obviously you don’t want anyway to ship your code with the tests binaries to bloat it.


This is (in theory) where the ‘Build configurations’ come into play: just exclude the *_test.cc files from Debug/Release, and equally the file containing the main() method of your program (I like to call those Main.cc and have the bare minimum to get it all started, but nothing more) be excluded from your Test configuration.


However, if you do that, and hence would not include the /include folder to all the Build Configurations (for which this would be rather pointless, as it would only be needed by the Test config) you lose the syntax highlight in all the files that make up your test fixture(s): for some reason, excluding a file from even ONE Build Configuration, confuses the hell out of CDT and the editor seems unable to cope even when the Active Configuration is the one (eg, Test) that you actually defined the file to be part of — this is bizarre, because the various configurations still build correctly and run as expected.


So here is the workaround (which is not pretty, but looks like it’s the only way to keep one’s sanity, and adds minimal hassle): just remember to add the gtest/include folder to all the build configurations settings.


A screenshot of what happens when you go for the “wholesome” approach and exclude from build all the *_test files as well as the gTest includes from the settings for the Debug/Release configurations [note however how the compilation/build completes without errors for the Test config]:






Let’s hope CDT 8.0 comes out soon, alongside XText 2.0 — all slated to be released along with Indigo (Eclipse 3.7) sometime in June 2011.

Advertisements

One thought on “Eclipse CDT and Google GTest

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s