gpt4 book ai didi

c - ANSI-C pow 函数和类型转换

转载 作者:行者123 更新时间:2023-12-04 05:53:32 25 4
gpt4 key购买 nike

下面这个简单的程序由于某种原因无法构建。它说“未定义对 pow 的引用”,但包含数学模块,我正在使用 -lm 标志构建它。如果我使用像 pow(2.0, 4.0) 这样的 pow,它就会构建,所以我怀疑我的类型转换有问题。

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

int main() {
int i;

for (i = 0; i < 10; i++) {
printf("2 to the power of %d = %f\n", i, pow(2.0, (double)i));
}

return EXIT_SUCCESS;
}

这是构建日志:
**** Build of configuration Debug for project hello ****
make all
Building file: ../src/hello.c
Invoking: GCC C Compiler
gcc -O0 -g -pedantic -Wall -c -lm -ansi -MMD -MP -MF"src/hello.d" -MT"src/hello.d" -o "src/hello.o" "../src/hello.c"
Finished building: ../src/hello.c

Building target: hello
Invoking: GCC C Linker
gcc -o "hello" ./src/hello.o
./src/hello.o: In function `main':
/home/my/workspace/hello/Debug/../src/hello.c:19: undefined reference to `pow'
collect2: ld returned 1 exit status
make: *** [hello] Error 1

**** Build Finished ****

最佳答案

你已经告诉它在错误的地方使用数学库——你在编译时指定了数学库(它不会有帮助),但在链接时(实际需要它的地方)却忽略了它。您需要在链接时指定它:

gcc -o "hello" ./src/hello.o -lm

关于c - ANSI-C pow 函数和类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9788996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com