[英]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.