【问题标题】:Header file wont work -undefined reference to `method'头文件不起作用 - 对“方法”的未定义引用
【发布时间】:2026-02-01 13:30:01
【问题描述】:

头文件似乎无法正常工作。 我是 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

【问题讨论】:

  • 每个列表是哪个文件?在将它们链接在一起之前,您不应该编译各个来源吗?
  • 编辑了像以前那样编译它们:(
  • 在执行 gcc findvals.o utils.o -o findvals 之前执行 gcc -c findvals.c
  • 最好还是自己写一个makefile。
  • 即使我按照正确的顺序进行操作,仍然无法正常工作。现在谷歌搜索makefile

标签: c gcc compilation


【解决方案1】:

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

gcc findvals.o utils.o -o findvals

检查这个。这将起作用。

【讨论】:

    【解决方案2】:

    使用如下所示的生成文件。

    .PHONY: all clean
    
    SRCS := findvals.c utils.c
    OBJS := $(SRCS:%.c=%.o)
    
    all: findvals
    
    findvals: $(OBJS)
    
    clean:
        rm -rf *.o findvals
    

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

    make
    

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

    【讨论】: