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

Posted by Daryl Spitzer on Stack Overflow See other posts from Stack Overflow or by Daryl Spitzer
Published on 2011-01-06T01:09:53Z Indexed on 2011/01/06 1:54 UTC
Read the original article Hit count: 335

Filed under:
|
|
|
|

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

When I use the Cmockery expect_string() macro like this:

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 that expect_string 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's 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'm compiling C code as C++, so the C++ compiler is objecting to (void*)string in the expect_string_count macro being passed as the const char* string parameter to the _expect_string() function.

I've already used extern "C" around the cmockery.h include in my_tests.cpp like this:

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

...in order to get around name-mangling problems. (See "How do I compile and link C++ code with compiled C code?")

Is there a command-line option or some other means of telling g++ how to relax its restrictions on typecasting from my test's C++ code to the C function in cmockery.c?

This is the command I'm currently using to build my_tests.cpp:

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

© Stack Overflow or respective owner

Related posts about c++

Related posts about c