我正在使用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]
}
^
答案 0 :(得分:1)
问题在您的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
被推送到文件范围,并被当作函数声明。