[英]Getting GCC error when using SQLite and Fast Math: “SQLite will not work correctly with the -ffast-math option of GCC”
[英]error In using math function in gcc?
當我在log2()中傳遞常量值時,如下所示
#include <stdio.h>
#include<math.h>
int main(int argc, char* argv[])
{
int var;
var= log2(16);
printf("%d",var);
return 0;
}
gcc prog.c(NO Error)4
但是當我在函數log2(var)中傳遞變量時,給出了對`log2'的錯誤未定義引用我需要鏈接庫即-lm
#include <stdio.h>
#include<math.h>
int main(int argc, char* argv[])
{
int var,i;
i= log2(var);
printf("%d",i);
return 0;
}
給出錯誤
undefined reference to `log2'
在第一段代碼中,編譯器用常量4
替換log2(16)
。 編譯器通常以這種方式優化常量數學。 這就是你沒有看到錯誤的原因。
請參閱生成的代碼以進行確認。 這是您的第一個版本:
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $32, %esp
movl $4, 28(%esp)
movl $.LC0, %eax
movl 28(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
movl $0, %eax
leave
ret
沒有調用log2。 編譯器已將其替換為常量4( movl $4, 28(%esp)
)。
這個是你的第二個版本:
main:
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $48, %esp
fildl 40(%esp)
fstpl (%esp)
call log2
fnstcw 30(%esp)
movzwl 30(%esp), %eax
movb $12, %ah
movw %ax, 28(%esp)
fldcw 28(%esp)
fistpl 44(%esp)
fldcw 30(%esp)
movl $.LC0, %eax
movl 44(%esp), %edx
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
movl $0, %eax
leave
ret
正如您所看到的,此版本中有一個call log2
。 這就是為什么第二個版本需要-lm
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.