Search code examples
cgccmingwglib

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


I am using GLib arrays. The code starts with the right include:

#include <gmodule.h>

I have an sorting function:

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

And a function where the array is used:

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);
}

However this does not compile.

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,
         ^~~~~~~~~~~~
...

This makes it look like it finds the prototype for g_array_sort only after it assumed a prototype. What is happening?

MCVE Code:

#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 error on compile:

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]
  }
  ^

Solution

  • The problem becomes evident in your MCVE. Take note of the ### comments I added.

    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;
    }
    

    They match opening an closing braces. So g_array_sort is pushed to file scope, and is taken as a function declaration.