Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当从C+;调用C函数时,我如何告诉gcc放松对类型转换的限制+;? 我试图用C++代码模拟C函数。因为SUT是C++,所以我的测试需要在C++中。_C++_C_Gcc_Casting_Cmockery - Fatal编程技术网

当从C+;调用C函数时,我如何告诉gcc放松对类型转换的限制+;? 我试图用C++代码模拟C函数。因为SUT是C++,所以我的测试需要在C++中。

当从C+;调用C函数时,我如何告诉gcc放松对类型转换的限制+;? 我试图用C++代码模拟C函数。因为SUT是C++,所以我的测试需要在C++中。,c++,c,gcc,casting,cmockery,C++,C,Gcc,Casting,Cmockery,当我使用Cmockery expect_string()宏时,如下所示: expect_string(mock_function, url, "Foo"); extern "C" { #include <cmockery.h> } 我得到: my_tests.cpp: In function ‘void test_some_stuff(void**)’: my_tests.cpp:72: error: invalid conversion from ‘void*’ to ‘con

当我使用Cmockery expect_string()宏时,如下所示:

expect_string(mock_function, url, "Foo");
extern "C" {
#include <cmockery.h>
}
我得到:

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)’
我看到expect_字符串定义如下:

#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)
下面是_expect_string的原型(来自cmockery.h):

<> P>我相信问题是我把C代码编译成C++,所以C++编译器反对在EXECUTSTRIGHYGULL宏中作为< <代码> const char字符串< /COD>参数传递到“γEpjString()”函数中的<代码>(空隙)字符串< /C>。 我已经在cmockery.h中使用了
extern“C”
,包括在my_tests.cpp中,如下所示:

expect_string(mock_function, url, "Foo");
extern "C" {
#include <cmockery.h>
}

我是不是SATAND,不是你的代码,但是看起来更容易的方法是通过将这个C++转换为<代码>(空洞*)< /C> >(可能只给C++使用了一些区段,只使用C++代码)。 这甚至可以放在代码中,只需重新定义

expect\u string\u count

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

我不认为在编译器级别有这样的选项。您可以通过使用CMockery的包装器头来解决这个问题(我假设您希望避免修改CMockery源代码,因为我在您的另一个问题中读到了注释),该包装器头执行以下操作,以使其在C和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 */

另外一个好处是,现在你有了一个地方,你可以把任何其他的黑客/修复程序都用于C++下(你希望不会有太多的)。 如果你准备修改CMockery的东西,那它可能真的属于你——也许维护人员会接受你的补丁?(我不知道)