c - Link error during gcc compile with makefile -
(before moving on,
the absence of frees , writing variables never used in codes,
intended testing tool)
wrote codes these , makefile this:
unread_two.h
#ifndef __unread_two_h #define __unread_two_h const int sizeof_int = sizeof(int); int addtwo();
unread_twomain.c
#include <stdio.h> #include <stdlib.h> #include "unread_two.h" int main(int argc, char *argv[]) { int *x; x = (int *)malloc(sizeof_int); x = addtwo(); free(x); return 0; }
unread_two.c
#include <stdio.h> #include <stdlib.h> #include <unread_two.h> int addtwo() { int *y, *z, sum; y = (int *)malloc(sizeof_int); z = (int *)malloc(sizeof_int); *y = 3; sum = *y + *y; return sum; }
makefile
cc=gcc ccflags=-g %.o: %.c $(cc) -c $< $(ccflags) all: unread_two clobber: clean rm -f *~ \#`\# core clean: rm -f unread_two *.o unread_two: unread_twomain.o unread_two.o unread_twomain.o: unread_two.h unread_two.o: unread_two.h
and when put make all, message appears:
unread_twomain.o:(.rodata+0x0): multiple definition of `sizeof_int' unread_two.o:(.rodata+0x0): first defined here collect2: error: ld returned 1 exit status
what things should fix?
you shouldn't defining sizeof_int
in header, otherwise you'll multiple definitions when include header in more 1 compilation unit, have seen. instead declare in header , define in source file:
// unread_two.h extern const int sizeof_int; // *declare* sizeof_int
// unread_two.c const int sizeof_int = sizeof(int); // *define* sizeof_int
alternatively in particular case might justified in doing "old skool" way, macro:
// unread_two.h #define sizeof_int sizeof(int)
Comments
Post a Comment