头文件不会工作-undefined参考`方法“ [英] Header file wont work -undefined reference to `method'
问题描述
头文件似乎并没有正常工作。
我是新的C和真的不知道我在做什么。
错误 - findvals.c :(文字+ 0x561):未定义的参考`approxEqual。
findvals.c
的#include<&time.h中GT;
#包括LT&;&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#包括utils.hINT主(INT ARGC,字符** argv的)
{
//输出(GRR ENTAR SOMETHANG);
//字符字[64];
// scanf函数(%S字);
诠释计数= 0;
INT RET;
双X;
双Y;
INT I = 0;
INT C = 0;
字符* refStr,* tolStr;
双refDub;
双tolDub;
INT noArgs = 0;
//双温;
// scanf函数(%LF,&安培;温度);
//输出(DUBBB%LF,温度); refStr = tolStr = NULL; //添加code读取参数,在-r任意顺序即之前或之后-t来
INT processCount = 0;
为(ⅰ= 1; I&小于5;我+ +)
{
如果(的strcmp(argv的[I]中,-r))
{
tolStr =的argv [I + 1];
我++;
//的printf(\\ NIM这里R);
}
否则如果(的strcmp(argv的[I]中,-t))
{
refStr =的argv [I + 1];
我++;
//的printf(\\ NIM HERE T);
}
} refDub = ATOF(refStr);
tolDub = ATOF(tolStr); //的printf(\\ nrefDub =%F,refDub);
//的printf(\\ ntolDub =%F,tolDub); //检查参数被正确传递。
如果(ARGC!= 5 || refStr == NULL || tolStr == NULL)
{
fprintf中(标准错误,用法:%s的-r -t参考TOL \\ n,argv的[0]);
出口(1);
} //添加code要注意的开始时间和日期,然后打印出来。
//现在只打印默认的字符串
结构TM *地方;
time_t的开始,结束;
时间(安培;启动); //读取和记录时钟
本地=本地时间(安培;启动);
的printf(\\ n#开始时间和日期:%S,asctime(本地));
//字符* TND =星期三2014年10月15日十九时18分13秒IST
//输出(#启动时间和日期:%S,TND);
//添加的功能性休息。 //读
诠释行; // Sixe x中的阵列的轴
诠释COLS; // Sixe Y轴澳阵列
scanf函数(%d个,&安培;行);
scanf函数(%d个,&安培; COLS);
双opArray [行] [COLS]; //数组的操作 对于(i = 0; I<行;我++)
{
为(C = 0;℃下COLS; C ++)
{
scanf函数(%LF,&安培; opArray [I] [C]);
}
} //读取在基质 双**垫=(双**)的malloc(sizeof的(双*)*行);
INT J = 0;
对于(i = 0; I<行;我++)
/ *分配阵列,存储指针* /
垫[i] =(双*)malloc的(的sizeof(双)* COLS); 对于(i = 0; I<行;我++)
{
为(J = 0; J< COLS; J ++)
{
scanf函数(%LF,&安培;垫[I] [J]);
}
} //下面的print语句应该是一个循环的一部分
//取消注释它,它调整到code,但只有一部分
//来后:\\ n // fprintf中(标准输出,R =%D,C =%d个:%.6f \\ n,R,C,行[R] [C]);
对于(i = 0; I<行;我++)
{
为(J = 0; J< COLS; J ++)
{
RET = approxEqual(垫[I] [J],refDub,tolDub);
如果(保留== 1)
{
fprintf中(标准输出,R =%D,C =%d个:%.6f \\ n,I,J,垫[I] [J]);
算上++;
}
}
}
//输出匹配的最后计数。同样,你可以只修改
//后打印语句的一部分:\\ n
// fprintf中(标准输出,找到%d个近似匹配\\ n,计数); //最后,打印出的结束时间和日期。现在只印刷
//默认的字符串。
时间(放大器端); //读取和记录时钟
本地=本地时间(放大器端);
的printf(\\ N#结束时间和日期:%S,asctime(本地)); 出口(0);
}
utils.c
的#includeutils.hINT approxEqual(双X,双R,双T)
{
INT上限= R + T;
INT地板= R-T; 如果(X>地板和放大器;&安培; X<天花板)
返回1;
其他
返回0;}
utils.h
#如果!定义(UTILS_H)
#定义UTILS_HINT approxEqual(双X,双R,双T);#万一
如果有人能在正确的方向指向我,会是很大的。
我如何编译它
GCC -c utils.c
GCC findvals.o utils.o -o findvals
GCC -c findvals.c
使用一个makefile,如下面所示。
.PHONY:都是干净的SRCS:= findvals.c utils.c
OBJS:= $(SRCS:%C =‰)所有:findvalsfindvals:$(OBJS)清洁:
室射频*的.o findvals
只需将该文件保存在同一目录作为源,称之为makefile文件或Makefile文件并使用命令
制作
作为一个方面说明。在你的主要功能,你应该添加一个检查柜面用户没有提供足够的命令行参数。你可以告诉提供多少使用argc变量,你不应该引用任何argv的值大于的 ARGC更高 - 1 的或你将有崩溃
Header File doesn't seem to work properly. I'm new to C and don't really know what i'm doing
error - findvals.c:(.text+0x561): undefined reference to `approxEqual'
findvals.c
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
int main(int argc, char **argv)
{
//printf("GRR ENTAR SOMETHANG");
//char word[64];
//scanf("%s", word);
int count = 0;
int ret;
double x;
double y;
int i = 0;
int c = 0;
char *refStr, *tolStr;
double refDub;
double tolDub;
int noArgs = 0;
//double temp;
//scanf("%lf" , &temp);
//printf("DUBBB %lf" , temp);
refStr = tolStr = NULL;
//Add code to read arguments, in ANY order i.e. -r can come before or after -t
int processCount = 0;
for(i = 1; i < 5; i++)
{
if (strcmp(argv[i], "-r"))
{
tolStr = argv[i+1];
i++;
//printf("\nIM HERE r");
}
else if (strcmp(argv[i], "-t"))
{
refStr = argv[i+1];
i++;
//printf("\nIM HERE t");
}
}
refDub = atof(refStr);
tolDub = atof(tolStr);
//printf("\nrefDub = %f", refDub);
//printf("\ntolDub = %f", tolDub);
//Check if arguments were passed in correctly.
if (argc != 5 || refStr == NULL || tolStr == NULL)
{
fprintf(stderr, "Usage: %s -r ref -t tol\n", argv[0]);
exit(1);
}
//Add code to note start time and date and then print it.
//Right now printing just a default string
struct tm *local;
time_t start, end;
time(&start); // read and record clock
local = localtime(&start);
printf("\n# Start time and date: %s", asctime(local));
//char * tnd="Wed 15 Oct 2014 19:18:13 IST";
//printf("# Start time and date: %s", tnd );
// Add rest of the functionality.
//READ
int rows; // Sixe x axis of the array
int cols; // Sixe y axis o the array
scanf("%d" , &rows);
scanf("%d" , &cols);
double opArray[rows][cols]; // Array for operations
for(i = 0 ; i < rows; i++)
{
for(c = 0 ; c < cols; c++)
{
scanf("%lf" , &opArray[i][c]);
}
}
//read in the matrix
double **mat = (double **) malloc(sizeof(double *)*rows);
int j=0;
for(i=0; i<rows; i++)
/* Allocate array, store pointer */
mat[i] = (double *) malloc(sizeof(double)*cols);
for(i=0; i<rows; i++)
{
for(j=0; j<cols; j++)
{
scanf("%lf",&mat[i][j]);
}
}
// The following print statement should be a part of a loop
// Uncomment it tailor it to your code but ONLY the part that
// comes after: \n",
// fprintf(stdout, "r=%d, c=%d: %.6f\n", r, c, rows[r][c]);
for(i= 0; i < rows ; i++)
{
for(j = 0 ; j <cols ; j++)
{
ret = approxEqual(mat[i][j],refDub,tolDub);
if (ret == 1)
{
fprintf(stdout, "r=%d, c=%d: %.6f\n", i, j, mat[i][j]);
count++;
}
}
}
// output the final count of matches. Again, you may ONLY modify
// the part of the print statement after: \n",
// fprintf(stdout, "Found %d approximate matches.\n", count);
//finally, print out the end time and date. Right now only printing
//a default string.
time(&end); // read and record clock
local = localtime(&end);
printf("\n# End time and date: %s", asctime(local));
exit(0);
}
utils.c
#include "utils.h"
int approxEqual(double x, double r, double t)
{
int ceiling = r+t;
int floored = r-t;
if(x > floored && x < ceiling)
return 1;
else
return 0;
}
utils.h
#if ! defined(UTILS_H)
#define UTILS_H
int approxEqual(double x, double r, double t);
#endif
If someone could point me in the right direction that'd be great
How i compiled it
gcc -c utils.c
gcc findvals.o utils.o -o findvals
gcc -c findvals.c
Use a makefile such as the one shown below.
.PHONY: all clean
SRCS := findvals.c utils.c
OBJS := $(SRCS:%.c=%.o)
all: findvals
findvals: $(OBJS)
clean:
rm -rf *.o findvals
Simply save this file in the same directory as your source, call it "makefile" or "Makefile" and use the command
make
As a side note. In your main function you should add a check incase the user does not provide enough command line arguments. You can tell how many are provided by using the argc variable and you should not reference any argv values higher than argc - 1 or you will have a crash.
这篇关于头文件不会工作-undefined参考`方法“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!