How to link two files using header file in C -
How to link two files using header file in C -
i trying link 2 files. means, there files "file1.c" , "file2.c".
file1.c
#include"stdlib.h" #include"stdio.h" void function1(int a) { printf("hello file%d.c\n ", a); } void main() { function1(1); }
file2.c
#include"stdlib.h" #include"stdio.h" #include"file.h" void function2(int b) { printf("hello file%d.c\n", b); } int main() { function2(2); function1(1); }
then create header file file.h as
#ifndef hell #define hell void function1(int a); #endif
when compile file2.c "gcc file2.c file1.c -o file2 " gives next error
/tmp/cc4tno9r.o: in function `main': file1.c:(.text+0x24): multiple definition of `main' /tmp/ccl4feki.o:file2.c:(.text+0x24): first defined here collect2: ld returned 1 exit status
how write in header file? there error in header file? or error in file2.c?
and extern? uses same purpose?
you don't need include library files in first file. save library file ".h" extension library file , include in sec file, shown below.
file1.h
void function1(int a) { printf("hello file%d.c\n ", a); }
file2.c
#include <stdlib.h> #include <stdio.h> #include "file.h" void function2(int b) { printf("hello file%d.c\n", b); } int main() { function2(2); function1(1); homecoming 0; }
c
Comments
Post a Comment