由于某些原因,下面这个简单的程序无法构建。它说“未定义的功率引用”,但数学模块也包含在内,我正在用-lm标志构建它。如果我使用pow(2.0,4.0)这样的能力,它就会构建,所以我怀疑我的类型转换有问题。
#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;
}下面是bulid日志:
**** 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 ****发布于 2012-03-20 22:47:53
您告诉它在错误的位置使用数学库--您在编译时指定了数学库(在这种情况下它不会起作用),但在链接时(在实际需要的地方)省略了数学库。您需要在链接时指定它:
gcc -o "hello" ./src/hello.o -lmhttps://stackoverflow.com/questions/9788996
复制相似问题