gpt4 book ai didi

c - 函数的隐式声明和 undefined reference

转载 作者:太空狗 更新时间:2023-10-29 12:43:29 28 4
gpt4 key购买 nike

关于编译suricata。在 Makefile 中,当 CFLAGS 包装“-Werror-implicit-function-declaration”时我得到了错误:

    detect-engine-siggroup.c: In function ‘SigGroupHeadFree’:
detect-engine-siggroup.c:187:9: error: implicit declaration of function ‘_mm_free’ [-Werror=implicit-function-declaration]
SCFreeAligned(sgh->mask_array);
^
detect-engine-siggroup.c: In function ‘SigGroupHeadBuildHeadArray’:
detect-engine-siggroup.c:1715:5: error: implicit declaration of function ‘_mm_malloc’ [-Werror=implicit-function-declaration]
sgh->mask_array = (SignatureMask *)SCMallocAligned((cnt * sizeof(SignatureMask)), 16);

当我在 Makefile 中删除“-Werror-implicit-function-declaration”时,我会得到错误:

detect-engine-siggroup.o: In function `SigGroupHeadFree':
/root/suricata/suricata-2.0.9/src/detect-engine-siggroup.c:187: undefined reference to `_mm_free'
detect-engine-siggroup.o: In function `SigGroupHeadBuildHeadArray':
/root/suricata/suricata-2.0.9/src/detect-engine-siggroup.c:1715: undefined reference to `_mm_malloc'

注意:_mm_free 和_mm_malloc 定义在util-mem.h然而,我在另一个源文件中添加了一些代码,但没有在 detect-engine-siggroup.c 和 util-mem.h 中添加。怎么了?

在detect-engine-siggroup.c中(注意我在这里删除了一些多余的代码):

void SigGroupHeadFree(SigGroupHead *sgh)
{
if (sgh == NULL)
return;

SCLogDebug("sgh %p", sgh);

PatternMatchDestroyGroup(sgh);

#if defined(__SSE3__) || defined(__tile__)
if (sgh->mask_array != NULL) {
/* mask is aligned */
SCFreeAligned(sgh->mask_array);
sgh->mask_array = NULL;
}
#endif

if (sgh->head_array != NULL) {
SCFree(sgh->head_array);
sgh->head_array = NULL;
}

if (sgh->match_array != NULL) {
detect_siggroup_matcharray_free_cnt++;
detect_siggroup_matcharray_memory -= (sgh->sig_cnt * sizeof(Signature *));
SCFree(sgh->match_array);
sgh->match_array = NULL;
}

sgh->sig_cnt = 0;

if (sgh->init != NULL) {
SigGroupHeadInitDataFree(sgh->init);
sgh->init = NULL;
}

SCFree(sgh);

detect_siggroup_head_free_cnt++;
detect_siggroup_head_memory -= sizeof(SigGroupHead);

return;
}

在util-mem.h中(注意我在这里删除了一些多余的代码):

#ifndef __UTIL_MEM_H__
#define __UTIL_MEM_H__

#include "util-atomic.h"

#if CPPCHECK==1
#define SCMalloc malloc
#define SCCalloc calloc
#define SCRealloc realloc
#define SCFree free
#define SCStrdup strdup
#define SCMallocAligned _mm_malloc
#define SCFreeAligned _mm_free
#else /* CPPCHECK */


#if defined(_WIN32) || defined(__WIN32)
#include "mm_malloc.h"
#endif

#if defined(__tile__)
/* Need to define __mm_ function alternatives, since these are SSE only.
*/
#include <malloc.h>
#define _mm_malloc(a,b) memalign((b),(a))
#define _mm_free(a) free((a))
#endif /* defined(__tile__) */

SC_ATOMIC_EXTERN(unsigned int, engine_stage);

/* Use this only if you want to debug memory allocation and free()
* It will log a lot of lines more, so think that is a performance killer */

/* Uncomment this if you want to print memory allocations and free's() */
//#define DBG_MEM_ALLOC

#ifdef DBG_MEM_ALLOC

#define SCFree(a) ({ \
extern uint8_t print_mem_flag; \
if (print_mem_flag == 1) { \
SCLogInfo("SCFree at %p", (a)); \
} \
free((a)); \
})

#else /* !DBG_MEM_ALLOC */


#define SCFree(a) ({ \
free(a); \
})

#if defined(__WIN32) || defined(_WIN32)

#define SCFreeAligned(a) ({ \
_mm_free(a); \
})

#else /* !win */

#define SCFreeAligned(a) ({ \
_mm_free((a)); \
})

#endif /* __WIN32 */

#endif /* DBG_MEM_ALLOC */

#endif /* CPPCHECK */

#endif /* __UTIL_MEM_H__ */

#endif /* DBG_MEM_ALLOC */

#endif /* CPPCHECK */

#endif /* __UTIL_MEM_H__ */

/*************************************************** ************************/

现在问题解决了,原因是忘记了 #include <pmmintrin.h> /* for SSE3 */

/*************************************************** ************************/

最佳答案

看起来您没有正确包含 util-mem.hSCFreeAligned 似乎扩展到 _mm_free,但 _mm_free 似乎没有扩展。查看头文件,_mm_free 的定义似乎取决于正在定义的 __title__

然后编译器只看到对 _mm_free 的调用,它被赋予隐式原型(prototype),因为 _mm_free 不存在。那么当然 _mm_free 将不会在链接过程中被找到。

关于c - 函数的隐式声明和 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33929926/

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