__extern_inline func导致未定义的引用错误

时间:2017-04-22 01:25:41

标签: c++ gcc clang

使用g ++ 4.8.5编译此c ++代码时使用-O0 -std = c ++ 11,

#include <stdio.h>
__extern_inline float func(float x) { return x*x; }
int main(int argc, char *argv[])
{
   printf("%d\n", func(100));
}

我收到此链接错误

/tmp/ccMiVRIL.o: In function `main':
test.cpp:(.text+0x18): undefined reference to `func(float)'
collect2: error: ld returned 1 exit status

但是,如果我在编译器选项中添加-O1或-O2,则会成功构建。 此外,如果我将“__extern_inline”更改为“extern __inline”或“__extern_always_inline”,它也会构建。我看到了与clang类似的行为。

我想拥有该代码(保持__extern_inline,因为该定义存在于第三方库中,我不想触摸)即使在-O0下也能够链接。我能做什么?是否有任何编译器选项或#defines来解决此错误?

我已阅读此文档,但它似乎对我的案例没有帮助 https://clang.llvm.org/compatibility.html#inline

在sys / cdefs.h中,__extern_inline的定义如下

#define __extern_inline extern __inline __attribute__ ((__gnu_inline__))

1 个答案:

答案 0 :(得分:0)

我认为这是因为跟随features.h中的宏:

/* Decide whether we can define 'extern inline' functions in headers.  */
#if __GNUC_PREREQ (2, 7) && defined __OPTIMIZE__ \
    && !defined __OPTIMIZE_SIZE__ && !defined __NO_INLINE__ \
    && defined __extern_inline
# define __USE_EXTERN_INLINES   1
#endif

如您所见,外部内联仅明确允许用于优化构建。

相关问题