头文件将无效 - 未定义对`method或#39;的引用

时间:2014-10-28 00:22:06

标签: c gcc compilation

头文件似乎无法正常工作。 我是C的新手,并不知道我在做什么

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

如果有人能指出我正确的方向,那将是伟大的

我如何编辑它

gcc -c utils.c
gcc findvals.o utils.o -o findvals
gcc -c findvals.c

2 个答案:

答案 0 :(得分:0)

使用如下所示的makefile。

.PHONY: all clean

SRCS := findvals.c utils.c
OBJS := $(SRCS:%.c=%.o)

all: findvals

findvals: $(OBJS)

clean:
    rm -rf *.o findvals

只需将此文件保存在与源相同的目录中,将其命名为&#34; makefile&#34;或&#34; Makefile&#34;并使用命令

make

作为旁注。在您的main函数中,您应该添加一个检查,因为用户没有提供足够的命令行参数。您可以通过使用argc变量来判断提供了多少,并且您不应该引用任何高于 argc - 1 的argv值,否则您将崩溃。

答案 1 :(得分:0)

gcc -c utils.c findvals.c utils.h

gcc findvals.o utils.o -o findvals

检查一下。这将有效。