How do I tell gcc to reduce typecasting restrictions when calling a C function from C ++?

I'm trying to use Cmockery to mock C functions called from C ++ code. Since SUT is in C ++, my tests should be in C ++.

When I use the Cmockery macro expect_string () as follows:

expect_string(mock_function, url, "Foo");

I get:

my_tests.cpp: In function β€˜void test_some_stuff(void**)’:
my_tests.cpp:72: error: invalid conversion from β€˜void*’ to β€˜const char*’
my_tests.cpp:72: error:   initializing argument 5 of β€˜void _expect_string(const char*, const char*, const char*, int, const char*, int)’

I see in cmockery.h for which the expect_string function is defined:

#define expect_string(function, parameter, string) \
    expect_string_count(function, parameter, string, 1)
#define expect_string_count(function, parameter, string, count) \
    _expect_string(#function, #parameter, __FILE__, __LINE__, (void*)string, \
                  count)

And here is the prototype for _expect_string (from cmockery.h):

void _expect_string(
    const char* const function, const char* const parameter,
    const char* const file, const int line, const char* string,
    const int count);

I believe the problem is that I am compiling C code as C ++, so the C ++ compiler objects (void*)stringto the expect_string_count macro passed as a parameter const char* stringto the _expect_string () function.

extern "C" cmockery.h my_tests.cpp, :

extern "C" {
#include <cmockery.h>
}

..., . (. " ++ C-?" )

- g++, ++ C cmockery.c?

, my_tests.cpp:

g++ -m32 -I ../cmockery-0.1.2 -c my_tests.cpp -o $(obj_dir)/my_tests.o
+3
2

, , - cmockery.h, (void*) ( , - ++, #ifdef __cplusplus).

, expect_string_count

#ifdef __cplusplus
#undef expect_string_count
#define expect_string_count(function, parameter, string, count) \
    _expect_string(#function, #parameter, __FILE__, __LINE__, string, \
              count)
#endif
+3

, . , ( , CMockery - , ), CMockery, - , C ++:

#ifndef MY_CMOCKERY_H
#define MY_CMOCKERY_H

/*
    A wrapper for cmockery.h that makes it C++ friendly while keeping things
    the same for plain-old C
 */

#if __cplusplus
extern "C" {
#endif

#include "cmockery.h"

#if __cplusplus
}
#endif


#if __cplusplus
// fixup CMockery stuff that breaks in C++

#undef expect_string_count
#define expect_string_count(function, parameter, string, count) \
    _expect_string(#function, #parameter, __FILE__, __LINE__, (char*)string, \
                  count)

#endif


#endif  /* MY_CMOCKERY_H */

, , / CMockery ++, (, ).

CMockery, , , , , , ? ( ).

+2

Source: https://habr.com/ru/post/1783770/


All Articles