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

标签 c

这是我的程序的一小部分,在 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/

相关文章:

c++ - SIGPIPE,断管

无法重新分配结构的 c 数组

c - 我如何使用 sscanf 获取带有/分隔符的整数

C - for 循环中的 strcat

c - C是否检查指针是否越界而不取消引用指针?

c++ - 在 C 或 C++ 中创建目录

c - 如何将ListView复选框与选择同步?(WIN32)

c - 在 gnuplot 和 C 中将绘图另存为 png

c - 如何使用fwrite写入数据0xf0f0f0f0

c - 提高通用交换的性能