4

I am trying to add unit testing framework cMockery to my C project. I have downloaded and installed the cMockery into global include path. But after #include <cmockery.h> in my source file, cmake throws the following issue. It seems the same thing will be thrown if I use cMocka as well. Am I missing some packages?

Edit: Google search for "/usr/include/google/cmockery.h:365:8: error: unknown type name ‘jmp_buf’" returned exactly 0 results (Now 1 result pointing to this question). As well as searching for unknown type name 'jmp_buf' only explains what is it. Not how to fix it or why does it happen inside cmockery.

/usr/bin/cmake --build /home/.../data-structures-c/cmake-build-debug --target data_structures_c -- -j 3
Scanning dependencies of target data_structures_c
[ 50%] Building C object CMakeFiles/data_structures_c.dir/main.c.o
In file included from /home/.../data-structures-c/main.c:3:
/usr/include/google/cmockery.h:365:8: error: unknown type name ‘jmp_buf’
 extern jmp_buf global_expect_assert_env;
        ^~~~~~~
make[3]: *** [CMakeFiles/data_structures_c.dir/build.make:63: CMakeFiles/data_structures_c.dir/main.c.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/data_structures_c.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/data_structures_c.dir/rule] Error 2
make: *** [Makefile:118: data_structures_c] Error 2

OS: Manjaro Archlinux

Cmake, make, gcc, g++ all installed.

My CMakeList.txt is also very standard, bare minimum with a single c source

cmake_minimum_required(VERSION 3.12)
project(data_structures_c C)
set(CMAKE_C_STANDARD 99)
add_executable(data_structures_c main.c)

I would like to know how to solve this issue in order to compile my code.

thuyein
  • 1,684
  • 13
  • 29
  • "Google search for "unknown type name 'jmp_buf'" returned exactly 0 results." - Not quite true: try to google for `unknown type name "jmp_buf"`. – Tsyvarev Apr 10 '19 at 15:15
  • Okay, the first result is this SO question itself. The Google statement I searched for was `/usr/include/google/cmockery.h:365:8: error: unknown type name ‘jmp_buf’`. It was zero results, but now 1 result to this question. `unknown type name 'jmp_buf'` only explains what is it. Not how to fix it. I'll modify my question to adjust the google query. – thuyein Apr 11 '19 at 00:50

1 Answers1

6

According to the comments at the beginning of google/cmockery.h header:

/*
 * These headers or their equivalents should be included prior to including
 * this header file.
 *
 * #include <stdarg.h>
 * #include <stddef.h>
 * #include <setjmp.h>
 *
 * This allows test applications to use custom definitions of C standard
 * library functions and types.
 */

Before including this header, one should include following headers:

#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>

Only after these includes it is allowable to

#include <google/cmockery.h>
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
  • Thank you, it works. I cannot believe I have forgotten to do just that. To read the header file source code. I was thinking about the missing packages or architecture problem the entire time. But normally, in this kind of case, shouldn't `cmockery.h` be the one including it's dependencies? – thuyein Apr 11 '19 at 13:10
  • Normally, a header itself includes everything what it needs. But in the given case, they have an explanation: "This allows test applications to use custom definitions of C standard library functions and types.". So CMockery can also be used on *free-standing* (opposite to *hosted*) implementations of C library, which doesn't provide common headers like `setjmp.h`. – Tsyvarev Apr 11 '19 at 13:31