【问题标题】:implicit declaration of function and undefined reference to函数的隐式声明和未定义的引用
【发布时间】:2015-11-26 03:05:24
【问题描述】:

关于编译 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 &lt;pmmintrin.h&gt; /* for SSE3 */

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

【问题讨论】:

  • 需要更多信息,隐式声明是由于缺少函数声明,删除该警告(或任何警告)是不好的,如果你这样做,你只是在隐藏问题。未定义的引用意味着也缺少定义。可以发一下代码吗?
  • 您似乎未能包含正确的头文件并且未能链接到正确的库。
  • 当我没有在其他.c文件中添加任何我的代码时,这两个源文件可以编译成功,
  • 你删除的冗余代码是什么?这一定是“它并不像你想象的那么多余”的第一个嫌疑人。似乎没有包含 util_mem.h 标头,或者没有以使您需要的声明可见的方式包含该标头。你需要找出原因。它甚至可以归结为检查来自 C 预处理器 (gcc -E …) 的预处理输出。 util-mem.h 末尾的 #endif 指令列表看起来很可疑——其中 3 个是重复的。
  • 感谢 Etan Reisner 和 Jonathan Leffler,现在我修复了这个错误。我使用 gcc -E 检查输出,发现我在其他地方删除了一些代码,导致 _mm_malloc 和 _mm_free 未定义.

标签: c linux makefile


【解决方案1】:

您似乎没有正确包含util-mem.hSCFreeAligned 似乎扩展为 _mm_free,但 _mm_free 似乎没有扩展。查看头文件_mm_free 的定义似乎依赖于正在定义的__title__

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

【讨论】:

    猜你喜欢
    • 2021-06-22
    • 2016-05-16
    • 1970-01-01
    • 2018-06-11
    • 2013-04-09
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多