由于某种原因,下面这个简单的程序无法构建。它说“未定义的对 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/