2014-10-28 2 views
0

Файл заголовка не работает должным образом. Я новичок в C и не знаю, что я делаюФайл заголовка не работает - привязка к методу `method '

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 
+0

В каком файле содержится листинг? И не следует ли компилировать отдельные источники, прежде чем связывать их вместе? –

+0

отредактировал компиляцию их так, как раньше :( – Will

+0

Сделайте gcc -c findvals.c, прежде чем вы выполните gcc findvals.o utils.o -o findvals –

ответ

0

Используйте Makefile, например, как показано ниже.

.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, и вы не должны ссылаться на любые значения argv выше argc - 1 или у вас будет сбой.

0

НКУ -c utils.c findvals.c utils.h

НКУ findvals.o utils.o -o findvals

Проверьте это. Это сработает.

Смежные вопросы