gcc - Using a function in differents C files -
i have prototype of function header.h defined :
void getsettings(); i'm using function in 1st source file file1.c :
void getsetting(){ printf(" in file 1 \n"); } this works. have second source file has use function, different implementation file2.c :
void getsetting() { printf(" in file 2 \n"); } the program crashes when start 2nd file, delivering segmentation fault (core dumped) error message. , doesn't implementation of second file. idea if i'm trying possible in c, if yes how ?
update compile code use :
gcc file2.c -o o2 and first file
gcc file1.c -o o1
yes, can must declare function static in header , in both files.
also, please not leave empty arguments in functions, instead use void, otherwise indicating function may variadic.
so want:
static void getsettings(void); and
static void getsettings(void) { printf(" in file 1 \n"); } and
static void getsettings(void) { printf(" in file 2 \n"); } also note, @invalid pointed out, may not want include prototype in header file function of file scope not need prototyped in header , may not need prototyped @ if defined before used.
Comments
Post a Comment