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

标签 c math casting ansi-c

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

相关文章:

c - C 的最佳 XML 解析器

c 数组中的不完整元素类型

c++ - 为什么计数整数不返回 d 的预期值?作为数字?

python - 在 Python 中编写这个 for 循环的最优雅的方法是什么?

c++ - 二次型矩阵与稀疏矩阵相乘的算法

c# - ToString() 和转换为字符串的区别

c# - 在 C# 中需要 IS 运算符

c - 在输入文件中使用数组,For和If结构

c - 哈希表中的段错误

c++ - 从结构到 LPVOID 的类型转换