Today I had an interview there they asked me can we include .c file
to a source file?
I said yes
. Because few years back I saw the same in some project where they have include .c file
. But just now I was trying the same.
abc.c
#include<stdio.h>
void abc()
{ printf("From ABC() \n"); }
main.c
#include<stdio.h>
#include "abc.c"
int main()
{ void abc();
return 0;
}
Getting an error:
D:\Embedded\...\abc.c :- multiple definition of 'abc'
Where is it going wrong?
I wrote an abc.h file (the body of abc.h is { extern void abc(void); }
),
and included the file in abc.c
(commenting out #include abc.c
). Worked fine.
You can properly include . C or . CPP files into other source files.
The answer to the above is yes. header files are simply files in which you can declare your own functions that you can use in your main program or these can be used while writing large C programs. NOTE:Header files generally contain definitions of data types, function prototypes and C preprocessor commands.
the biggest disadvantage in including . c files is that you can declare main twice.
You have to call the function from the "main" file. The including file should be "file_name. c" not "file_name.
Do it as follows:
abc.c
:
#include <stdio.h>
void abc()
{printf("From ABC() \n");}
main.c
:
#include<stdio.h>
#include "abc.c"
int main()
{
abc();
return 0;
}
(no need for the header file)
Then, to compile, you'd only compile main.c
. Do not attempt to compile both abc.c
and main.c
, because then you'd have the abc()
function defined twice.
You need to understand that #include
is basically "copy-paste", nothing more. If you tell it #include "abc.c"
, it will simply take the contents of abc.c
, and "paste" them in your main.c
file. Therefore, using the above for main.c
, after the preprocessor processes it, your main.c
will look like this (I'm ignoring the #include <stdio.h>
s):
#include<stdio.h>
#include <stdio.h>
void abc()
{printf("From ABC() \n");}
int main()
{
abc();
return 0;
}
which is a valid program.
That said you should generally not do this; you should compile all your .c
files separately and only then link them together.
Including C files is perfectly valid as long as you do not try to compile the included C file by itself and then link it together with the object file from the C file which included the other file.
If you do so you'll have the same symbol (usually a function) defined in two files which will result in the errors you posted.
If you have multiple source files you usually do not include them but compile them separately. The linker then merges the object files into a single executable (or library).
You can include file with any extension.
In your program, you had re-defined void abc();
in main (). instead just put statement abc ();
You can include anything you like to, the preprocessor doesn't care about the file extensions. It's only some tradition to name the headers ".h" and the source files ".c" or ".cpp".
You only have to be sure, that after compiling the whole project you don't run into linker problems (e.g. giving both "abc.c" and "main.c" to the compiler would result in multiple definitions of your function).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With