UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Explorer
Explorer
7,243 Views
Registered: ‎10-25-2012

Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

I just find a question which confused me. In the template project: lwIP Echo Server, Inside 'src' folder, there are "main.c" and "echo.c" files. In "main.c", the main function use some functions inside "echo.c" file such as "int start_application()"

 

However, I did not find any commands inside "main.c" to include the "echo.c" file and there is no corresponding header file for "echo.c". But why the functions can be still used inside the main function?

 

Hope somebody can help me to understand this, thanks advance!

0 Kudos
1 Solution

Accepted Solutions
Voyager
Voyager
9,282 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

This is a C programming concept and not related to the environment. 

 

First things first ..The use of a header file is to include the function prototypes! So that implicite decleration is avoided..Now the function prototype int start_application() is include in the main file itself! U will find it just below the include statements.

 

That is why there is no echo.h because all the functions defined in echo.c file which are called in the main are declated as prototypes in the main file..

 

And as far as including echo.c goes there is no such thing! when u build a project with all the source files it complies everything ..So as long as u have the function prototype and the defination they can be access any function defined in any file.. Its easier to use header files because you dont have to declare the prototypes every time rather you can just include the corresponding file containing the prototypes. 

 

Hope this helps

0 Kudos
16 Replies
Explorer
Explorer
7,230 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Anybody can help?
0 Kudos
Explorer
Explorer
7,227 Views
Registered: ‎03-31-2011

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
The way u stated ur problem,that makes me confused too..BTW go with xapp1026 for LWIP
0 Kudos
Explorer
Explorer
7,223 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
I am sorry my statement confused you, Let me state it again and may be more clear.

There are "main.c" and "echo.c" two source files in LWIP project. In "main.c", main function call functions inside "echo.c" file such as "int start_application()"

However, I did not find "main.c" include the "echo.c" file and there is no corresponding header file for "echo.c". So I can not understand why the functions can be still called inside "main.c"?

BTW, what is xapp 1026 you mentioned?

Thanks very much.
0 Kudos
Explorer
Explorer
7,216 Views
Registered: ‎03-31-2011

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

If two .c files use the same project directory then their functions can be  used as such way..

 

xapp1026 is xilinx application note available as .pdf format....just make Google search with xapp1026,u shall get that pdf

0 Kudos
Explorer
Explorer
7,210 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Thanks, that makes sense. Is this rule general fro all C development in any IDE or just for IDE like Xilinx SDK?

Thanks again.
0 Kudos
Explorer
Explorer
7,191 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Hi, I checked the xapp 1026 file. Inside Lwip, there is an application "TCP RX Throughput Test and TCP TX Throughput Test". I just wonder is this application can use Ethernet to transmit and receive data?
0 Kudos
Voyager
Voyager
9,283 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

This is a C programming concept and not related to the environment. 

 

First things first ..The use of a header file is to include the function prototypes! So that implicite decleration is avoided..Now the function prototype int start_application() is include in the main file itself! U will find it just below the include statements.

 

That is why there is no echo.h because all the functions defined in echo.c file which are called in the main are declated as prototypes in the main file..

 

And as far as including echo.c goes there is no such thing! when u build a project with all the source files it complies everything ..So as long as u have the function prototype and the defination they can be access any function defined in any file.. Its easier to use header files because you dont have to declare the prototypes every time rather you can just include the corresponding file containing the prototypes. 

 

Hope this helps

0 Kudos
Explorer
Explorer
7,174 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Thanks, ur explanation is very clear. But if I want to do implicite decleration, the source code which has these function prototypes must be inside the same project folder, right?

Otherwise, the compiler can not know where the functions are.
0 Kudos
Voyager
Voyager
7,167 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

I am not sure I understood your question correctly. But implicite decleration of functions are not allowed in C. U have to declare the functions explicitly. 

0 Kudos
Explorer
Explorer
5,132 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Now I think I got it. The function declaration like "int start_application()" is explicitly declaration. If there is no corresponding header file for "echo.c" and I do not declare the function "int start_application()", but I call it in "main.c". implicite declaration will happen.
0 Kudos
Voyager
Voyager
5,129 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

Yes that is EXACTLY what happens. 

0 Kudos
Explorer
Explorer
5,123 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
It seems in SDK, if you have implicite declaration,it will report warning instead of error.
0 Kudos
Voyager
Voyager
5,120 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

Yes it does report it as a warning as strictly speaking its not an error. But u can change the option in the build settings to promote all warnings to errors. That way you will solve all warnings before finalizing the program. But this can be tedious so be warned. 

0 Kudos
Explorer
Explorer
5,112 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Got it, thanks!
0 Kudos
Voyager
Voyager
5,107 Views
Registered: ‎02-10-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution

Okay , good. Please close the thread by accepting what ever post was in direct relation to your original question as the solution. Would help others in the future to go to the solution more quickly . 

 

regards

Arvind

0 Kudos
Explorer
Explorer
5,103 Views
Registered: ‎10-25-2012

Re: Why the source file (.c) can use the function in another source file without include the file?

Jump to solution
Thanks, Arvind. I will.
0 Kudos