c - function undefined even though it is in a different .h file -


i getting compile error saying functions undefined. of these functions defined in file called auxilarity.h , implemented in auxilarity.c

i can't seem find i'm missing.

here makefile:

assembly: main.o assembler.o hashtable.o  auxilarity.o     gcc -g -ansi -wall -pedantic main.o assembler.o hashtable.o -o assembly  auxilarity.o: auxilarity.c  auxilarity.h     gcc -c -ansi -wall -pedantic auxilarity.c -o auxilarity.o  main.o: main.c assembly.h hash.h     gcc -c -ansi -wall -pedantic main.c -o main.o  assembler.o: assembler.c assembly.h auxilarity.h     gcc -c -ansi -wall -pedantic assembler.c -o assembler.o  hashtable.o: hashtable.c auxilarity.h hash.h     gcc -c -ansi -wall -pedantic hashtable.c -o hashtable.o 

and relevant parts of:

assembler.c:

#include "assembly.h" #include "hash.h" #include "auxilarity.h"  ... code ...  int isargvalid(char *s) {     char *ns;     ns = cleanstr(s); /* <-- compiler yells on cleanstr undefined*/      if(ns[0] == 'r' && ns[1] >= '0' && ns[1] <= '8' && strlen(ns) == 2) {         printf("argument %s register", s);         return arg_register;     }     return 1;  }  ...  code  ... 

auxilarity.h:

#ifndef auxilarity_h_ #define auxilarity_h_  char *cleanstr(char *s); char *_strdup(char *s);  #endif /* auxilarity_h_ */ 

and here auxilarity.c:

#include <string.h> #include <stdlib.h> #include "auxilarity.h"  /*remove leading , following spaces*/ char *cleanstr(char *s) {         int i, j, k;         char *copy;         for(i = 0; s[i] == ' ' && s[i] == '\t'; i++)             ;         for(k = i; s[i] != '\0'; k++) {             if(s[i] != ' ' && s[i] != '\t')                 j =k;         }          copy = (char *) malloc(j-i);         memcpy(copy, s, j-i);         return copy; }  char *_strdup(char *s) {     char *p;      p = (char *) malloc(strlen(s) + 1);     if (p != null)         strcpy(p, s);     return p; } 

here exact error messages i'm getting (it's in french, sorry):

hashtable.c:48:9: warning: implicit declaration of function ‘free’ [-wimplicit-function-declaration]          free((void *) np->defn); /*free previous defn */          ^ hashtable.c:48:9: warning: incompatible implicit declaration of built-in function ‘free’ [enabled default] gcc -c -ansi -wall -pedantic auxilarity.c -o auxilarity.o gcc -g -ansi -wall -pedantic main.o assembler.o hashtable.o -o assembly assembler.o: dans la fonction « parseinstruction »: assembler.c:(.text+0x15): référence indéfinie vers « _strdup » assembler.o: dans la fonction « isargvalid »: assembler.c:(.text+0x240): référence indéfinie vers « cleanstr » hashtable.o: dans la fonction « put »: hashtable.c:(.text+0x123): référence indéfinie vers « _strdup » collect2: error: ld returned 1 exit status make: *** [assembly] erreur 1 

i looked on , can't find i'm doing wrong

(i'm getting implicit function definition warning built-in functions malloc despite have included relevant .h files)

you have problem here:

assembly: main.o assembler.o hashtable.o  auxilarity.o     gcc -g -ansi -wall -pedantic main.o assembler.o hashtable.o -o assembly 

you list auxilarity.o dependency don't link in command line.

you should use macros more:

object = main.o assembler.o hashtable.o auxilarity.o  assembly: ${object}     gcc -g -ansi -wall -pedantic ${object} -o $@ 

using macro ensures consistency. should using still more macros, compiler , of options. @ least ensures consistency.


actually, repeated compilation lines problematic. make has built-in rules compile code; use them.

cflags  = -g -ansi -wall -pedantic -werror object  = main.o assembler.o hashtable.o auxilarity.o ldflags = ldlibs  =  assembly: ${object}     ${cc} ${cflags} ${object} -o $@ ${ldflags} ${ldlibs}  auxilarity.o: auxilarity.c  auxilarity.h main.o:       main.c assembly.h hash.h assembler.o:  assembler.c assembly.h auxilarity.h hashtable.o:  hashtable.c auxilarity.h hash.h 

and when listing dependencies, make infer main.o depends on main.c don't have list (though there's no harm done if do, , completeness suggests ok). need list headers, though.

and regarding undeclared function warning, make sure include <stdlib.h> in file. since assembler.h includes <stdlib.h> hashtable.c not include assembler.h, need put #include <stdlib.h> hashtable.c.

generally, should include use (iwyu). equally, headers should self-contained. there numerous questions on topic on so, won't go repeating answers them.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -