警告:内置函数log2不兼容的隐式声明。

时间:2021-10-30 06:58:41

So we have this program in C, we need to use the base-2 logarithmic function, to get the base 2 logarithm of n. Here's the code:

我们在C中有这个程序,我们需要使用base-2对数函数,得到底2的对数,这是代码:

#include <math.h>

int partSize(int n) {
    return log2(n);
}

But when compiling, it gives us the following warning.

但是在编译时,它给了我们以下警告。

sim.c: In function partSize : sim.c:114: warning: incompatible implicit declaration of built-in function log2

sim卡。c:在功能部分:sim。c:114:警告:内置函数log2的隐式声明不兼容。

This is the command we use

这是我们使用的命令。

 gcc $file -o $name.out -lm 

1 个解决方案

#1


7  

Here's the thing, 99.99999% of the time, when someone says "this basic function that's available to the world doesn't work" they're wrong. The fraction of the time when something this basic breaks, there's already an army with pitchforks somewhere.

这是99.99999%的时间,当有人说“这个世界上可用的基本功能不起作用”时,他们错了。在这个基本的休息时间里,已经有一支军队在某处有了干草叉。

#include <math.h>
#include <stdio.h>

int partSize(int n){
    return log2(n);
}

int main(int argc, char *argv[]) {
    int ret = -1;
    ret = partSize(16);
    printf("%d\n", ret);
    return 0;
}

Compile with:

编译:

 > gcc -std=c99 a.c -o log2.out -lm
 > ./log2.out 
 > 4

Yup, it's working.

是的,这是工作。

In C, using a previously undeclared function constitutes an implicit declaration of the function. In an implicit declaration, the return type is int. So the error tells you that log2() was not defined in your code that leads to some issue in the code you didn't post.

在C中,使用之前未声明的函数构成了函数的隐式声明。在隐式声明中,返回类型是int,因此错误告诉您,在您的代码中没有定义log2(),这导致了代码中没有发布的一些问题。

When I skip the -lm I get:

当我跳过-lm,我得到:

a.c:(.text+0x11): undefined reference to `log2'
collect2: ld returned 1 exit status

a.c:(.text+0x11):未定义的引用“log2”collect2: ld返回1退出状态。

..that doesn't look right. OK, when I add the -lm but remove the #include <math.h> I get:

. .这看起来不正确的。好的,当我添加-lm,但是移除#include 我得到: 。h>

a.c: In function ‘partSize’:
a.c:5:5: warning: implicit declaration of function ‘log2’ [-Wimplicit-function-declaration]

一个。c:在功能“partSize”:a。c:5:5:警告:隐式声明函数' log2 ' [- wim- function-declaration]

Hey, there's your warning! So you're probably correct that you're including the -lm but for some reason the #include math.h has a problem. Could be that:

嘿,你的警告!所以你可能是正确的,包括-lm,但由于某些原因,包括数学。有一个难题。可能是:

  1. math.h is missing
  2. 数学。h是失踪
  3. you didn't really include it in the file, is it in a #def and being compiled out for example?
  4. 你没有把它包含在文件中,是在#def中吗?
  5. Your version of math.h doesn't define log2
  6. 你的版本的数学。h不定义log2

#1


7  

Here's the thing, 99.99999% of the time, when someone says "this basic function that's available to the world doesn't work" they're wrong. The fraction of the time when something this basic breaks, there's already an army with pitchforks somewhere.

这是99.99999%的时间,当有人说“这个世界上可用的基本功能不起作用”时,他们错了。在这个基本的休息时间里,已经有一支军队在某处有了干草叉。

#include <math.h>
#include <stdio.h>

int partSize(int n){
    return log2(n);
}

int main(int argc, char *argv[]) {
    int ret = -1;
    ret = partSize(16);
    printf("%d\n", ret);
    return 0;
}

Compile with:

编译:

 > gcc -std=c99 a.c -o log2.out -lm
 > ./log2.out 
 > 4

Yup, it's working.

是的,这是工作。

In C, using a previously undeclared function constitutes an implicit declaration of the function. In an implicit declaration, the return type is int. So the error tells you that log2() was not defined in your code that leads to some issue in the code you didn't post.

在C中,使用之前未声明的函数构成了函数的隐式声明。在隐式声明中,返回类型是int,因此错误告诉您,在您的代码中没有定义log2(),这导致了代码中没有发布的一些问题。

When I skip the -lm I get:

当我跳过-lm,我得到:

a.c:(.text+0x11): undefined reference to `log2'
collect2: ld returned 1 exit status

a.c:(.text+0x11):未定义的引用“log2”collect2: ld返回1退出状态。

..that doesn't look right. OK, when I add the -lm but remove the #include <math.h> I get:

. .这看起来不正确的。好的,当我添加-lm,但是移除#include 我得到: 。h>

a.c: In function ‘partSize’:
a.c:5:5: warning: implicit declaration of function ‘log2’ [-Wimplicit-function-declaration]

一个。c:在功能“partSize”:a。c:5:5:警告:隐式声明函数' log2 ' [- wim- function-declaration]

Hey, there's your warning! So you're probably correct that you're including the -lm but for some reason the #include math.h has a problem. Could be that:

嘿,你的警告!所以你可能是正确的,包括-lm,但由于某些原因,包括数学。有一个难题。可能是:

  1. math.h is missing
  2. 数学。h是失踪
  3. you didn't really include it in the file, is it in a #def and being compiled out for example?
  4. 你没有把它包含在文件中,是在#def中吗?
  5. Your version of math.h doesn't define log2
  6. 你的版本的数学。h不定义log2