Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't we include .c file?

Tags:

c++

c

include

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.

like image 1000
Rasmi Ranjan Nayak Avatar asked Jul 04 '12 19:07

Rasmi Ranjan Nayak


People also ask

Can a .C file be included?

You can properly include . C or . CPP files into other source files.

Can we include .C file in header?

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.

Why shouldnt you include a .C source file?

the biggest disadvantage in including . c files is that you can declare main twice.

Can we include .C file in another .C file?

You have to call the function from the "main" file. The including file should be "file_name. c" not "file_name.


4 Answers

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.

like image 186
houbysoft Avatar answered Sep 27 '22 17:09

houbysoft


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).

like image 37
ThiefMaster Avatar answered Sep 27 '22 18:09

ThiefMaster


You can include file with any extension.

In your program, you had re-defined void abc(); in main (). instead just put statement abc ();

like image 31
Sach Avatar answered Sep 27 '22 17:09

Sach


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).

like image 40
C. Stoll Avatar answered Sep 27 '22 16:09

C. Stoll