Cmake C Example

Oct 18, 2020 For these reasons, since version 1.10.42060 of our C library, we added a CMakeLists.txt file in the Sources subdirectory. This file contains all the compilation rules for our library. To use our C library in a CMake project, you only need to include this file and to reference our library with the yoctolib name. A short example. In the above example, 609281088cfefc76f9d0ce82e1ff6c30cc3591e5 is the Git commit hash of the GoogleTest version to use; we recommend updating the hash often to point to the latest version. For more information about how to create CMakeLists.txt files, see the CMake Tutorial. Create and run a binary.

This tutorial aims to get you up and running with GoogleTest using CMake. Ifyou’re using GoogleTest for the first time or need a refresher, we recommendthis tutorial as a starting point. If your project uses Bazel, see theQuickstart for Bazel instead.

Prerequisites

To complete this tutorial, you’ll need:

Cmake_c_flagsCmake C Example
  • A compatible operating system (e.g. Linux, macOS, Windows).
  • A compatible C++ compiler that supports at least C++11.
  • CMake and a compatible build tool for building theproject.
    • Compatible build tools includeMake,Ninja, and others - seeCMake Generatorsfor more information.

See Supported Platforms for more information about platformscompatible with GoogleTest.

If you don’t already have CMake installed, see theCMake installation guide.

Cmake Example Github

Example

Cmake Example C Project

Note: The terminal commands in this tutorial show a Unix shell prompt, but thecommands work on the Windows command line as well.

Set up a project

By Martin Hořeňovský Jun 2nd 2018 Tags: CMake, Tutorial, C. My previous post about CMake provided a simple CMakeLists.txt for a small, self-contained, project. In practice, very few projects are fully self-contained, as they either depend on external libraries or are themselves libraries that other projects depend on. CMake C example project for depthai-core library. CMake example project which serves as a template on how to quickly get started with C and depthai library. Depthai library dependencies. Cmake = 3.2; libusb1 development package; C/C11 compiler; MacOS: brew install libusb. Linux: sudo apt install libusb-1.0-0-dev.

The option -DCMAKEBUILDTYPE=Release specify the build type.We can also use Debug or RelWithDebInfo etc. To build the project, we can use cmake -build build if we are in project root or cmake -build. If we are in directory build.If you generate Makefile for make, you can also use make -C build to build the project directly, or use ninja -C buildninja if you generate the build file for ninja.

CMake uses a file named CMakeLists.txt to configure the build system for aproject. You’ll use this file to set up your project and declare a dependency onGoogleTest.

Cmake c example pdf

First, create a directory for your project:

Next, you’ll create the CMakeLists.txt file and declare a dependency onGoogleTest. There are many ways to express dependencies in the CMake ecosystem;in this quickstart, you’ll use theFetchContent CMake module.To do this, in your project directory (my_project), create a file namedCMakeLists.txt with the following contents:

The above configuration declares a dependency on GoogleTest which is downloadedfrom GitHub. In the above example, 609281088cfefc76f9d0ce82e1ff6c30cc3591e5 isthe Git commit hash of the GoogleTest version to use; we recommend updating thehash often to point to the latest version.

Cmake C Compiler

For more information about how to create CMakeLists.txt files, see theCMake Tutorial.

Cmake

Create and run a binary

With GoogleTest declared as a dependency, you can use GoogleTest code withinyour own project.

Cmake C Flags

As an example, create a file named hello_test.cc in your my_projectdirectory with the following contents:

GoogleTest provides assertions that you use to test thebehavior of your code. The above sample includes the main GoogleTest header fileand demonstrates some basic assertions.

To build the code, add the following to the end of your CMakeLists.txt file:

The above configuration enables testing in CMake, declares the C++ test binaryyou want to build (hello_test), and links it to GoogleTest (gtest_main). Thelast two lines enable CMake’s test runner to discover the tests included in thebinary, using theGoogleTest CMake module.

Now you can build and run your test:

Congratulations! You’ve successfully built and run a test binary usingGoogleTest.

Next steps

  • Check out the Primer to start learning how to write simpletests.
  • See the code samples for more examples showing how to use avariety of GoogleTest features.