• AVR Freaks
Reply to post

Helpful ReplyHot!Confusion on Header Files to Connect Multiple C Source Files

Author
racingsubby
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2010/06/22 10:34:11
  • Location: 0
  • Status: offline
2020/02/17 11:13:38 (permalink)
0

Confusion on Header Files to Connect Multiple C Source Files

Ok, I've tried to find an answer to this on the web but I get a mixed response.  I got a general C programming question.  If I have functions in another source file, call it functions.c, and I have the functions declared in the header file, call it functions.h, I know I have to include the header file in my main.c file.  This makes sense.  BUT does it also have to be listed at the top of the source file its supports??  In this example, do I need to have #include "functions.h" at the top of the functions.c source file as well as the main? 
Using MCC, I the header files included in their supporting source files.  For example, it creates the Initialize.c file.  within that, I see #include "Initialize.h".  I thought this only needed to be in the main.c file or am I mistaken?
Any clarity on this would be appreciated!
Thanks,
Tom
ric
Super Member
  • Total Posts : 26159
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Confusion on Header Files to Connect Multiple C Source Files 2020/02/17 12:22:48 (permalink) ☄ Helpfulby racingsubby 2020/02/20 07:54:58
0
racingsubby
...
does it also have to be listed at the top of the source file its supports??  In this example, do I need to have #include "functions.h" at the top of the functions.c source file as well as the main?

Yes, it should be.
That then confirms that all the definitions in the header file match up with your real functions.
You will get a compile error if they don't.
Plus, any #define lines you want both files to see should be in there.
 

Using MCC, I the header files included in their supporting source files.  For example, it creates the Initialize.c file.  within that, I see #include "Initialize.h".  I thought this only needed to be in the main.c file or am I mistaken?

You're mistaken.
 
Have a read of this file (ignore the C++ specific parts)
Organizing Code Files in C and C++

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
LdB_ECM
Super Member
  • Total Posts : 313
  • Reward points : 0
  • Joined: 2019/04/16 22:01:25
  • Location: 0
  • Status: offline
Re: Confusion on Header Files to Connect Multiple C Source Files 2020/02/17 17:25:28 (permalink) ☄ Helpfulby racingsubby 2020/02/20 07:55:15
0
You can't answer the question definitively and you will get different answers to this. What happens is compiler dependent, how the compile process is organized and if it is a C++ compiler playing at C.
 
I get caught all the time from Visual Studio when I take projects from it onto an embedded C compiler. Ever since VS existed it carries the header files globally you only need it included somewhere in some obscure unit and every subsequent unit can find it's definition which is exactly what the OP expected. On VS you can do exactly what the OP suggested include the headers once.
 
Basically it comes down to whether the compiler caries existing .h file reads globally and that comes down to the compile process. VS for example reads everything in a huge lump and then only compiles the code at the end or when it hits some memory limit.  Where a makefile process determines exactly what is read and when the compile takes place.
 
So all we can say is when using PIC compilers you have to include headers in each and every unit and use guards to prevent multiple inclusion.
racingsubby
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2010/06/22 10:34:11
  • Location: 0
  • Status: offline
Re: Confusion on Header Files to Connect Multiple C Source Files 2020/02/20 07:50:12 (permalink)
0
Thanks for the response.  I figured it was compiler dependent.  So to be safe, I should include headers in their respective source files and put guards.  I appreciate it.
racingsubby
Starting Member
  • Total Posts : 57
  • Reward points : 0
  • Joined: 2010/06/22 10:34:11
  • Location: 0
  • Status: offline
Re: Confusion on Header Files to Connect Multiple C Source Files 2020/02/20 07:55:03 (permalink)
0
This is good info.  I appreciate it.  I started a new project and used MCC to auto generate the configuration files.  I see the mcc.c source file includes its respective mcc.h file.  The same for the pin_manager.c and adc.c.  So I'll keep with that.  I appreciate the explanation on why this is.  That helps!  The more I understand why things are, the more it makes sense to me!
Thanks!
 
mlp
boots too small
  • Total Posts : 889
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: Confusion on Header Files to Connect Multiple C Source Files 2020/02/20 10:14:50 (permalink)
0
LdB_ECM
What happens is compiler dependent

What happens in C is defined by the C language standard, which in this particular respect remains unchanged since 1989.
(Edit to add) I'm pretty sure C++ had the same behaviour when it first came into existence, but I have not followed the changes in the language over the last couple decades.
 
Ever since VS existed it carries the header files globally you only need it included somewhere in some obscure unit and every subsequent unit can find it's definition

Then it's not a C compiler.
If it claims to be, then you are entitled to a refund.
If it does not claim to be a C compiler, then how it behaves is irrelevant here.
post edited by mlp - 2020/02/20 10:18:07

Mark (this opinion available for hire)
Guest
Quick Reply: (Open Full Version)
  Enter the random characters shown
Submit Post
Some restrictions apply to prevent link (URL) Spam.
URLs in messages, signatures, and PM's are removed unless you have ...
  • been a member for at least 0 day(s);
  • made a total of 0 post(s);
  • earned at least 0 point(s) for post scores (based on the ratings on your posts);
  • earned at least 0 reward point(s);
Jump to:
© 2020 APG vNext Commercial Version 4.5