C - multiple definition of function during building large project -
i'm trying build large project , in have following header file
error.h
#ifndef __error_h__ #define __error_h__ #include <stdio.h> #include <stdlib.h> void error_validate_pointer(void *ptr) { if (ptr == null) { puts("error allocating memory"); exit(1); } } #endif /* __error_h__ */
i use function in every .c file have (i include "error.h" in every file) thought #ifndef protect me multiple definition error. yet, during building, following errors:
../dictionary/libdictionary.a(state_list.c.o): in function `error_validate_pointer': /home/pgolinski/dokumenty/programowanie/spellcheck-pg359186/src/dictionary/error.h:8: multiple definition of `error_validate_pointer' ../dictionary/libdictionary.a(hint.c.o):/home/pgolinski/dokumenty/programowanie/spellcheck-pg359186/src/dictionary/error.h:8: first defined here ../dictionary/libdictionary.a(state_set.c.o): in function `error_validate_pointer': /home/pgolinski/dokumenty/programowanie/spellcheck-pg359186/src/dictionary/error.h:8: multiple definition of `error_validate_pointer' ../dictionary/libdictionary.a(hint.c.o):/home/pgolinski/dokumenty/programowanie/spellcheck-pg359186/src/dictionary/error.h:8: first defined here
what reason keep getting these errors? how avoid it?
it's happening because #include
definition several times, end having multiple definitions - 1 every file include in.
change code to
#ifndef __error_h__ #define __error_h__ #include <stdio.h> #include <stdlib.h> void error_validate_pointer(void *ptr); #endif /* __error_h__ */
... header, contains declaration of function.
then create new file (e.g. error.c
) definition
#include "error.h" void error_validate_pointer(void *ptr) { if (ptr == null) { puts("error allocating memory"); exit(1); } }
Comments
Post a Comment