我在使用 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/