c - 对 `pow' 的 undefined reference - makefile

标签 c makefile linker

我在使用 makefile 构建程序时遇到问题。

运行时make我收到错误:

ScanData.o: In function `CheckGrade':
ScanData.c:(.text+0x1ef): undefined reference to `pow'

这是生成文件:

a.out: Update.o ScanData.o ReallocAndFree.o PrintStudentMaxGrade.o PrintAllStudents.o DeleteStudent.o AddStudent.o AddOrUpdate.o main.o
 gcc Update.o ScanData.o ReallocAndFree.o PrintStudentMaxGrade.o PrintAllStudents.o DeleteStudent.o AddStudent.o AddOrUpdate.c main.o

AddOrUpdate.o: AddOrUpdate.c AddOrUpdate.h
    gcc -c AddOrUpdate.c

AddStudent.o: AddStudent.c AddStudent.h
    gcc -c AddStudent.c

DeleteStudent.o: DeleteStudent.c DeleteStudent.h
    gcc -c DeleteStudent.c

PrintAllStudents.o: PrintAllStudents.c PrintAllStudents.h
    gcc -c PrintAllStudents.c

PrintStudentMaxGrade.o: PrintStudentMaxGrade.c PrintStudentMaxGrade.h
    gcc -c PrintStudentMaxGrade.c

ReallocAndFree.o: ReallocAndFree.c ReallocAndFree.h
    gcc -c ReallocAndFree.c

ScanData.o: ScanData.c ScanData.h
    gcc -c ScanData.c -lm

Update.o: Update.c Update.h
    gcc -c Update.c

main.o: main.c AddOrUpdate.c AddStudent.c DeleteStudent.c PrintAllStudents.c PrintStudentMaxGrade.c ReallocAndFree.c ScanData.c Update.c
    gcc -c main.c

谢谢。

最佳答案

pow 是一个 stdlib 函数,但要使用它,您应该链接 stdlib 本身(数学库)。

通常 gcc 会自动执行此操作,但请尝试添加 -lm 链接标志:

gcc -lm Update.o ScanData.o <..the rest of object files..>

还要确保系统中存在 libc(在 debian/ubuntu 上 - dpkg -s libc6-dev 应该表明它已安装)

与问题没有直接关系: main.o 真的依赖于所有源吗?它仅从 main.c

构建

关于c - 对 `pow' 的 undefined reference - makefile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34439063/

相关文章:

c - 这段C代码的bug在哪里

linux - 交叉编译 glibc 导致构建错误

c++ - 使用 CMakeLists.txt 链接 C++ 文件

c - 首先显示文件内容然后覆盖它的程序--->给出段错误

c - 为什么我的 C 程序会产生奇怪的 printf 输出?

linux - srlua makefile 错误 lua.h No such file or directory

c - "--oformat=elf32-i386"有什么用?

c++ - 静态链接库

c - 如何使用 recv 逐行读取 html 页面?

android - 每个 NDK 构建都是一个完整的重建