Header file wont work -undefined reference to `method'

时光怂恿深爱的人放手 提交于 2019-12-13 06:55:17

问题


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

回答1:


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

gcc findvals.o utils.o -o findvals

Check this. This will work.




回答2:


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.



来源:https://stackoverflow.com/questions/26599004/header-file-wont-work-undefined-reference-to-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!