函数的隐式声明和未定义的引用

时间:2015-11-26 03:05:24

标签: c linux makefile

关于编译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 */

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

1 个答案:

答案 0 :(得分:0)

您好像未正确包含util-mem.hSCFreeAligned似乎已扩展为_mm_free,但_mm_free似乎无法扩展。查看头文件,_mm_free的定义似乎取决于__title__的定义。

然后编译器只看到对_mm_free的调用,由于_mm_free不存在而给出了隐式原型。当然,在链接过程中找不到_mm_free