gpt4 book ai didi

c - Googletest 编译在 #imported 文件中定义结构时失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:08:37 25 4
gpt4 key购买 nike

我有两个文件,first_pass.c,其中包含描述 mystring.cmystring.h。在测试 first_pass.c 时,我执行了以下操作

#include "../src/mystring.c"
#include "../src/first_pass.c"
#include <gtest/gtest.h>

一切正常,直到我不得不在 mystring.h 中定义一个结构。就这么简单

typedef struct Split {
char *head;
char *tail;
}Split;

然后我在编译时开始失败 error: ‘Split’ has a previous declaration as ‘typedef struct Split Split’

类(class)的要求是使用 C(不是 C++)和 MAKE,因此不推荐使用其他测试框架,例如 Check。我知道它定义了两次结构,第一次是在加载 mystring.c 时定义的,另一次是在 first_pass.c 上定义的。我该怎么办?

最佳答案

使用 header 保护来防止包含 header 不止一次。

http://en.wikipedia.org/wiki/Include_guard

一个建议将头文件添加到 C 文件而不是 C 文件到另一个 C 文件。

关于c - Googletest 编译在 #imported 文件中定义结构时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745976/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com