Googletest Cmake

broken image


Jan 27, 2014 Integrating googletest into CMake project. Putting googletest into your source tree; Adding googletest to your toplevel CMakeLists.txt to build it as part of your project: addsubdirectory(gtest-1.7.0) Adding the directory with your (future) tests to your toplevel CMakeLists.txt: addsubdirectory(test) Creating a CMakeLists.txt for the test. Googletest is available as a git repository which you can clone and then copy into your project. You could go all fancy with CMake scripts to download it if it's not already in your project, but since you can clone once and copy later on I choose not to automate it. Use CMake to download GoogleTest as part of the build‘s configure step. This is just a little more complex, but doesn't have the limitations of the other methods. If you want to run CMake to build a project as part of a test, you can do that too (in fact, this is how CMake tests itself). Chanel surfing.net. For example, if your master project was called MyProject and you had an examples/simple project that could build by itself, this would look like.

This post is about how to cross-compile GoogleTest in Eclipse.

Environment
  • Ubuntu 16.04 LTS
  • Eclipse Neon.3 Release 4.6.3
Install CMake
Checkout GoogleTest
Configure ARM toolchain

How to install ARM toolchain see Raspberry Pi: C++ cross-compiling.

  • Create in the root of GoogleTest folder toolchain-arm-linux-gnueabihf.cmake file with the following content:

Googletest Cmake Tutorial

  • Configure CMake:
Create Eclipse project

Import the project in Eclipse:

  • File > Import
  • Select:
    • General
    • Existing Project into Workspace
    • Next
  • Select project
    • Select root directory > Browse (to the GoogleTest folder)
    • Finish
Compile GoogleTest

Eclipse:

Project > Build project

Check library type

How to install googletest library?

Setting up googletest (or gtest, C++ unit testing library) can be tricky. When starting a new project, integrating googletest can be problematic, despite having no such issues with other libraries.

The following guide provides instructions on how to compile and add googletest to a C++ project. It uses pkg-config to obtain compiler flags and works with build systems such as cmake.

It was tested on Ubuntu but might work on other systems as well.

Download and compile googletest

Download the latest release from https://github.com/google/googletest/releases

Plex video. and extract it:

After that, we can compile it:

Now, copy the headers to a directory where compilers can find them:

Cmake

Then, copy static libraries:

Additionally, you can also add googlemock library (C++ framework for mocking objects):

Find googletest with pkg-config

After compiling and installing library files, try to find the library with pkg-config:

If you get a message about gtest not being found ('Package gtest was not found in the pkg-config search path') then it means pkg-config cannot find gtest's .pc file which provides the necessary configuration.

Run the following command to see the list of directories read by pkg-config:

Choose one of them, e.g. /usr/lib/pkgconfig, and inside that directory create a file named gtest.pc which should like similar to this:

Having done that, the following command:

should produce valid output, i.e. similar to

Integrating googletest with cmake

Googletest Cmake Github

If you use cmake to build your program, you can use pkg-config to find correct compilation flags.

First, in order to add support for pkg-config in cmake, add the following in CMakeLists.txt:

Cmake Google Test Example

Then, find gtest with pkg-config:

You can verify that it's available by printing GTEST_FOUND variable

and running cmake . on your project.

After you confirm cmake can find the library, just add a definition for your test executable, e.g.

and then link gtest with it:

Running build command e.g. cmake . && make should now successfully build the executable.

Googletest Cmake Tutorial

That concludes googletest installation. See the next section if you need to add googlemock (C++ mocking library). Visual studio code java.

Integrating googlemock with a C++ project

Googletest Cmake Add_test

First, ensure you have copied libgmock*.a libraries to /usr/local/lib, i.e. by executing:

Create gmock.pc configuration file in /usr/lib/pkgconfig so that pkg-config can resolve compiler flags:

Then, find gmock in CMakeLists.txt:

Finally, link your tests with googlemock by adding ${GMOCK_LIBRARIES} variable:

Googletest Cmake Deprecation Warning

After all those steps, tests using googletest/googlemock should compile correctly by executing standard commands (cmake . && make)!

Related posts





broken image