gpt4 book ai didi

c - 为什么使用 MSVC 可以工作,而使用 GNU 会出现错误 : array type has incomplete element type using struct

转载 作者:行者123 更新时间:2023-11-30 19:17:39 25 4
gpt4 key购买 nike

这是我的程序的一小部分,在 Visual Studio、gcc 和另一个编译器上运行。尝试编译下面的代码时,我从 gcc 编译器中得到以下错误:

In file included from myData.h:5:0,
from main.c:2:
def.h:14:39: error: array type has incomplete element type
extern CONST_MACRO_DEF( struct foo, myData[] );
^
def.h:10:63: note: in definition of macro 'CONST_MACRO_DEF'
#define CONST_MACRO_DEF( arg_type, arg_name) const arg_type arg_name __attribute__((section(".INFO")))

文件宏.h

#ifndef MACRO_H_
#define MACRO_H_

#include <stdio.h>


#if defined (_MSC_VER)
#pragma section(".INFO")
#define CONST_MACRO_DEF( arg_type, arg_name) const arg_type __declspec(allocate(".INFO")) arg_name
#elif defined( __GNUC__ )
#define CONST_MACRO_DEF( arg_type, arg_name) const arg_type arg_name __attribute__((section(".INFO")))
#else
#define CONST_MACRO_DEF( arg_type, arg_name) const arg_type arg_name
#endif

struct foo;

extern CONST_MACRO_DEF( struct foo, myData[] );

#endif /* MACRO_H_ */

文件 myData.h

#ifndef MY_DATA_H_
#define MY_DATA_H_

#include "def.h"

struct foo
{
int nummer;
double myHight;
};


CONST_MACRO_DEF( struct foo, myData[] ) =
{
{ 1 , 5.5 },
{ 2 , 6.0 },
{ 3 , 7.9 }
};

#endif

文件main.c

#include <stdio.h>
#include "myData.h"

int main(void)
{

return 0;
}

备注:

  • 真正的代码是用Visual Studio编译成功的,但用GCC编译器编译不成功。
  • 如果我将 Macro.h 中的 struct foo 从不完整类型更改为完整类型,方法是将其定义从 myData.h 移至 def.h 中,则可以使用 gcc 编译代码。 (我更喜欢一个将 Macro.h 中的 struct foo 保留为不完整类型的解决方案)

最佳答案

差异可能在于 declspec(allocate(".INFO")) 和 __attribute((section(".INFO"))) 的处理。

现在这些东西显然是不标准的。您可能应该检查 MSVC 和 GCC 文档。

关于 gcc 节属性应该用于变量,而不是 extern 声明,因此会出现与不完整类型相关的错误。

关于c - 为什么使用 MSVC 可以工作,而使用 GNU 会出现错误 : array type has incomplete element type using struct,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870518/

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