Connect with us

Including header files

Discussion in 'Microcontrollers, Programming and IoT' started by Sadlercomfort, Nov 16, 2015.

Scroll to continue with content
  1. Sadlercomfort

    Sadlercomfort Ash

    424
    56
    Feb 9, 2013
    Hi Guys,

    Does anyone know how to use header files to simplify programming.

    I'm using MPLAB and HI-TECH compiler.. programming in C.


    I've managed to create my .h file with the C programming inside, so how do i include that in a general program?
     
  2. Minder

    Minder

    3,215
    700
    Apr 24, 2015
  3. hevans1944

    hevans1944 Hop - AC8NS

    4,703
    2,202
    Jun 21, 2012
    Perhaps the operating manual for the compiler might offer a clue. Or check out this page of Frequently Asked Questions. And microchip has a nice forum where you can find answers to questions like yours.

    I'm sorry, I don't do much C (or pay money for compilers), but maybe @chopnhack will see this and reply. He is becoming quite adept at rolling C code.:cool:
     
  4. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Hi Sadler, he is saying that tongue in cheek. I would rewrite the above statement and substitute adept with inept. For real coding help get with Supercap2F, I nominate him our coding guru at the moment.

    Long and short, create your header file with definitions, make sure its in the same directory as your other files or you will have to point the compiler to it.
    Include (pun intended :p ) a line in your main.c that looks like this:

    #include "your_header_name_here.h"
     
    Last edited: Nov 17, 2015
    Supercap2F and hevans1944 like this.
  5. hevans1944

    hevans1944 Hop - AC8NS

    4,703
    2,202
    Jun 21, 2012
    @chopnhack : It's all relative. Maybe you aren't up there with @Supercap2F yet, but you are way above where I am as a C programmer.

    Thanks for explaining for @Sadlercomfort how to use an #include statement.

    If I ever start playing with some of those really sophisticated PICs you are dipping into, I guess I will have to learn to write programs in C or C++ or whatever the programming language du jour is. Abstraction and pointers help to keep programs organized, if not exactly optimized.

    I have been avoiding C since about 1989, or thereabouts, when we used a real-time variant of Unix to control a re-build of a 1950s era six degree-of-freedom space-environment testing system, used to characterize classified overhead sensor platforms. That was almost a two-year task, but I had a genius at my disposal to write the C code while I concentrated on the new hardware. We couldn't have done the job without him, and I learned the value of a team effort to get big things accomplished.

    I have a Pentium 4 that is getting a little long in the tooth running Windows XP Professional SP-3. One of these days I will move the application software and data files to a new machine I built a few years ago and convert this machine to Linux, probably the Ubuntu distro. Then I will take a stab at writing programs in C or C++. The really nice thing about Linux is it runs on just about anything Intel. For now, the C++ version used with the Arduino Uno is enough for me to putter with, although not having an operating system on the Arduino is a real PITA.
     
    chopnhack likes this.
  6. Sadlercomfort

    Sadlercomfort Ash

    424
    56
    Feb 9, 2013
    Thanks guys and @chopnhack :D

    I originally learnt assembly in University but converted to C at work. :confused:

    You wont believe the similarities, once you learn the syntax its pretty straight forward.


    I'll give this ago now :)
     
  7. Sadlercomfort

    Sadlercomfort Ash

    424
    56
    Feb 9, 2013
    It works :D

    Does it matter where i place the #Include <header_name.h>
     
  8. Supercap2F

    Supercap2F

    550
    151
    Mar 22, 2014
    Header files should never contain code - Only Defines/Typedefs/Function prototypes/etc. Where you put your code is in .c files.

    So, first right click on the Header Files folder of your current project and select: "New -> C Header File."
    Project.JPG
    Then fill in the "New C Header File" window. Now make a new .c source file in the "Source Files" folder with the same name as the header file you just created. In your main.c file add the statement: "#include "MyHeader.h" "(where MyHeader is the header file you just created). The header file is in quotes instead of these: <> because it's a local file to the project. Also add the same statement in the .c source file you just created.

    Now, your going to need whats called a "inclusion guard" in your MyHeader.h file, so it isn't complied twice (since it's included in two .c files: main.c and MyHeader.c). Here's how a inclusion guard looks:
    Code:
    #ifndef MYHEADER_H
    #define MYHEADER_H
    
    // add all your Defines/Typedefs/Function prototypes/etc. here
    
    #endif
    And that's all. Put all of your code that you want in a separate file in the MyHeader.c file. All functions should be prototyped in the MyHeader.h file only, and all your #defines should be in there too.

    When the compiler sees that there is a MyHeader.h file it will look at the source file folder for a matching .c file. When it sees it, it simply compiles it along with the rest of the code. So you don't need to put a "#include "MyHeader.c" " anywhere in your code.

    If all that was kind of confusing, then have a look here: http://www.gamedev.net/page/resourc...amming/organizing-code-files-in-c-and-c-r3173
    That's how I figured out how to do all this stuff, and you might want to read it even if you did understand my explanation. :p:D
    Dan
     
    hevans1944 and chopnhack like this.
  9. Sadlercomfort

    Sadlercomfort Ash

    424
    56
    Feb 9, 2013
    Thanks Dan,

    I don't quite understand the use of an inclusion guard.

    Let me have a read through the entire tutorial :D I like to read slowly and from start to finish so it sinks in.. might take me a few days.

    I'll let you know how I get on.


    PS. Turns out my first attempt at using header files didn't really work (It contained code). :rolleyes:
     
  10. Harald Kapp

    Harald Kapp Moderator Moderator

    12,449
    2,981
    Nov 17, 2011
    The inclusion guard is there to prevent multiple definitions or redefinition of the same definition. This may otherwise happen if an include file needs to reference another include file or a C file #includes multiple include files that have the same definition.

    The first line of the guard is
    #ifndef MYHEADER_H // this line checks whether this include file has already been processed, in that case the name "MYHEADER_H" is defined and the rest of the definitions is skipped up to and including the line
    #endif

    The second line of the guard is
    #define MYHEADER_H // this line defines the name "MYHEADER_H" so other #includes of this file will see it defined, see previous paragraph.

    The rest between #define and #endif
    // add all your Defines/Typedefs/Function prototypes/etc.
    here means exactly what Dan has written

    While it is not good style to have code within a header, it doesn't necessarily mean it won't work. The C preprocessor and the C compiler don't care as long as the resulting syntax of the C code (with all processed includes) is o.k.

    I hope that helps a bit along your way.
     
    Last edited: Nov 19, 2015
    hevans1944 and Supercap2F like this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-