Setting up Eclipse CDT and Google Test for C development

This post Builds on my previous post, https://bjornarnelid.wordpress.com/2014/03/10/how-to-get-started-with-google-test-in-eclipse-cdt-on-linux/ . Follow that guide to install Google Test and Eclipse CDT.

I really like Google Test, its great as a test framework, and i really like working in it when doing TDD style developing in C++. When it comes to C the choice of testing framework is not as easy. There are things like CUnit and check, but for me they don’t give as good Eclipse integration and flexibility as Google Test does. So wouldn’t it be great if you could test your C code using Google Test?

It turns out that you can, and its not as difficult as you might think, here is how!

Install Eclipse CDT and Google Test as usual

If you dont know how to do thism, dont worry! Just follow my previous guide here!

Create your Test Project

This will be quite similar to how you create your C++ Project in my previous post.

  1. Create a new C++ project in Eclipse using File -> New -> C++ Project
  2. Since this will only be your test project you only need to create one source folder.
  3. Go into properties for the test folder by right clicking on the folder and selecting properties
  4. Select C/C++ Build ->Settings
  5. Under GCC C++ Compiler -> Includes. Add <google test folder>/include
  6. Under GCC C++ Linker -> Libraries. Add the google test build folder and the libraries gtest and pthread
  7. If Eclipse still shows errors, you can try to rebuild your index.

Create your Application Project

Create a C project as usual where you will develop your application. Make a separate c file containing an empty main method, alternative move your main method to a separate file.

Doing this will let you compile your application project as usual and import application logic into your test project.

Setup your Test Project To Work With Your Application Project

  1. Go to YourProject -> Properties -> C/C++ Build -> Settings -> Includes
  2. Add you C application source folder as a include path
  3. Go to GCC C++ Linker Miscellaneous
  4. Add your C project .o files as Other objects

You are now ready for development! Its not as straight forward as the C++ solution, but its working well enough.

Leave a comment