[英]c multiple source files return bad value
我正在为我的Linux课程做一些作业,其中一项作业教给我们有关Makefile的使用。 我已经设法使我创建的makefile能够正常运行,所以不用担心,您不会失去我的知识。 也就是说,我对程序的行为感到困惑。
我正在使用makefile将4个源文件编译到一个程序中。 3个源文件由一个函数组成,该函数实际上仅使用math.h中的标准函数执行计算,并返回一个双精度值。 例如:
#include <math.h>
double sqroot(double arg)
{
arg = (sqrt(arg));
return arg;
}
一个源文件调用所有这三个函数。
#include <math.h>
double sum(double arg)
{
arg = (sqroot(arg) + square(arg) + sine(arg));
return arg;
}
主要内容如下:
#include <stdio.h>
void main(int argc, char *argv[])
{
double num=atoi(argv[1]);
printf("the sine of %lf in radians is %lf\n", num, (sine(num)));
printf("the square root of %lf is %lf\n", num, sqroot(num));
printf("the square of %lf is %lf\n", num, square(num));
printf("the sum of all of these is %lf\n", sum(num));
}
该程序编译没有问题,但是当我运行该程序时,它会打印错误的值。
使用GDB,我检查了每个函数的返回变量的值,这些值正确无误,但是进入主函数后,该值不相同。 当我简单地将这些函数与构造函数一起放置在主体中时,程序将按预期运行。
样本终端输出:
eng-svr-1:/home/jhazel/Desktop/Linux/257/hw8>make all
gcc -c main.c sine.c sqrt.c square.c sum.c -lm
gcc -o HEYTHERE main.o sine.o sqrt.o square.o sum.o -lm
gcc -g main.c sine.c sqrt.c square.c sum.c -lm
eng-svr-1:/home/jhazel/Desktop/Linux/257/hw8>./HEYTHERE 2
the sine of 2.000000 in radians is 0.000000
the square root of 2.000000 is 0.000000
the square of 2.000000 is 0.000000
the sum of all of these is 0.000000
eng-svr-1:/home/jhazel/Desktop/Linux/257/hw8>
我的makefile构造不正确吗? 用不正确的命令编译? 还是我的源文件中缺少某些内容?
由于MikeCat建议在我的makefile文件中使用-Wall -Wextra
,因此我能够确定我没有包括其他源文件中实现的功能的声明。
为了解决该问题,我包括了一个带有声明的头文件:
double square(double arg);
double sine(double arg);
double sqroot(double arg);
到总和和主要源文件,以及另外的double sum(double arg);
到主要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.