警告:数据定义没有类型或存储 class 尽管包括 header

Warning: data definition has no type or storage class despite including header

我正在使用 GLib 数组。右边开头的代码包括:

#include <gmodule.h>

我有排序功能:

gint key_sort(gconstpointer a, gconstpointer b) {...}

以及使用数组的函数:

GArray * score_keysizes( ... )
{
   GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));
   for (...)
   {
     ...      
     g_array_append_val(keys,temp);
     ...
   }
   g_array_sort(keys,key_sort);
}

然而这并不能编译。

gcc -c -g -Wall -Wextra -mms-bitfields -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include -o Crypto.o Crypto.c
Crypto.c:345:3: warning: data definition has no type or storage class
 g_array_sort(keys,key_sort);
 ^~~~~~~~~~~~
Crypto.c:345:3: warning: type defaults to 'int' in declaration of 'g_array_sort' [-Wimplicit-int]
Crypto.c:345:3: warning: parameter names (without types) in function declaration
Crypto.c:345:3: error: conflicting types for 'g_array_sort'
In file included from C:/msys64/mingw64/include/glib-2.0/glib.h:31,
             from C:/msys64/mingw64/include/glib-2.0/gmodule.h:28,
             from Crypto.c:7:
C:/msys64/mingw64/include/glib-2.0/glib/garray.h:114:9: note: previous declaration of 'g_array_sort' was here
 void    g_array_sort              (GArray           *array,
         ^~~~~~~~~~~~
...

这使得它看起来像是在假定原型后才找到 g_array_sort 的原型。发生了什么事?

MCVE 代码:

#include <stdio.h>
#include <stdlib.h>
#include <gmodule.h>

float hamdist_keyscore(const unsigned char *txtdat, unsigned int txtsize, unsigned int keysize);


typedef struct{
  unsigned int key;
  float score;
}key_candidate;

gint key_sort(gconstpointer a, gconstpointer b)
{
  if (((key_candidate *)a)->score > ((key_candidate *)(b))->score) return 1;
  else if (((key_candidate *)a)->score < ((key_candidate *)(b))->score) return -1;
  /* if (a.score == b.score)*/ return 0;  

}

GArray * score_keysizes(unsigned char* crypt, unsigned int len, unsigned int minkeysize, unsigned int maxkeysize)
{

  GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));

  for (unsigned int i = minkeysize; i <= maxkeysize; i++)
  {
      key_candidate temp;
      temp.score = hamdist_keyscore(crypt, len, i);
      temp.key = i;
      g_array_append_val(keys,temp);
    }
  }

  g_array_sort(keys,key_sort);

  return keys;
}

MCVE 编译错误:

gcc -c -g -Wall -Wextra -mms-bitfields -IC:/msys64/mingw64/include/glib-2.0 -IC:/msys64/mingw64/lib/glib-2.0/include -IC:/msys64/mingw64/include -o Crypto.test.o Crypto.test.c
Crypto.test.c:35:3: warning: data definition has no type or storage class
   g_array_sort(keys,key_sort);
   ^~~~~~~~~~~~
Crypto.test.c:35:3: warning: type defaults to 'int' in declaration of 'g_array_sort' [-Wimplicit-int]
Crypto.test.c:35:3: warning: parameter names (without types) in function declaration
Crypto.test.c:35:3: error: conflicting types for 'g_array_sort'
In file included from C:/msys64/mingw64/include/glib-2.0/glib.h:31,
                 from C:/msys64/mingw64/include/glib-2.0/gmodule.h:28,
                 from Crypto.test.c:3:
C:/msys64/mingw64/include/glib-2.0/glib/garray.h:114:9: note: previous declaration of 'g_array_sort' was here
 void    g_array_sort              (GArray           *array,
         ^~~~~~~~~~~~
Crypto.test.c:37:2: error: expected identifier or '(' before 'return'
  return keys;
  ^~~~~~
Crypto.test.c:38:1: error: expected identifier or '(' before '}' token
 }
 ^
Crypto.test.c: In function 'score_keysizes':
Crypto.test.c:33:2: warning: control reaches end of non-void function [-Wreturn-type]
  }
  ^

问题在您的 MCVE 中变得很明显。请注意我添加的 ### 评论。

GArray * score_keysizes(unsigned char* crypt, unsigned int len, unsigned int minkeysize, unsigned int maxkeysize)
{ // ### 1

  GArray * keys = g_array_new (FALSE, FALSE, sizeof(key_candidate));

  for (unsigned int i = minkeysize; i <= maxkeysize; i++)
  { // ### 2
      key_candidate temp;
      temp.score = hamdist_keyscore(crypt, len, i);
      temp.key = i;
      g_array_append_val(keys,temp);
    } // ### 2
  } // ### 1

  g_array_sort(keys,key_sort);

  return keys;
}

它们匹配左大括号。所以 g_array_sort 被推送到文件范围,并被视为函数声明。