NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Make header file

July 17, 2011
by MetalSoftForScience
MetalSoftForScience's Avatar

Hi. I was just wondering how to make a header file to include in some of my projects.

July 17, 2011
by Ralphxyz
Ralphxyz's Avatar

You literally allready know.

Here is how my OS X IDE (Xfile) did it for me:

/*
 *  untitled.c
 *  
 *
 *  Created by Ralph on 7/17/11.
 *  Copyright 2011 Ralph Hulslander.... All rights reserved.
 *
 */

#include "untitled.h"

This is from untitled.c you just include your header file.

The contents of your header file is another discussion ;-)

Ralph

July 17, 2011
by Robotnik
Robotnik's Avatar

Here is an example.

just make a file called file_name.h. Lets say it has a function in it called add...

// file_name.h

int add(int a, int b)
{
  return a + b;
}

then to include that file into your project you would include it in like this

    // the header file you made
    #include "file_name.h"

    int main()
    {
      int result = add(2,2) // calls the function from the header file

      while(1)
      {
      }
      return 0;
    }
July 17, 2011
by Robotnik
Robotnik's Avatar

oops, how embarrassing... I forgot a semicolon :/

int result = add(2,2);
July 18, 2011
by MetalSoftForScience
MetalSoftForScience's Avatar

I understand once I have the <something>.h file, how to include that in my code. I just don't understand how to make the file be a .h file. I tried renaming it but that changed it to <something.h>.c. Can somebody please help with how to actually make the ".h" file.

July 18, 2011
by Ralphxyz
Ralphxyz's Avatar

MetalSoftForScience, what ever you used to rename your .c file to .h has a default setting to always append .c to any new file save.

You have to look for an override of the default setting.

You probable are using some sort of ide to make your file projects.

You also might be able to use a text file editor to make your .h file and then use your ide to edit it.

If you look at your settings there is a override.

What OS are you on?

Ralph

July 18, 2011
by MetalSoftForScience
MetalSoftForScience's Avatar

I am using windows 7 professional. To rename it, I just renamed it. I tried opening it in notepad and would then save as <something>.h. It wouldn't work, just appendaged that ".c" anyway.

July 18, 2011
by Ralphxyz
Ralphxyz's Avatar

Notepad will do ugly things don't even bother trying to use it.

You can download NotePad++ it is a terrific text editor plus it gives you syntax highlighting.

There is a Programmers NotePad in the Nerdkits download that is often used. It is already installed on your computer.

Ralph

July 18, 2011
by MetalSoftForScience
MetalSoftForScience's Avatar

So in Programmers NotePad, just save with the header file extension?

July 19, 2011
by Ralphxyz
Ralphxyz's Avatar

Most likely, I most use my Mac mini only occasionally Windows and Programmers NotePad.

I think you will really like Programmers NotePad.

Of course there is also AVR Studio from Atmel but that gets complicated so I'd stick with Programmers NotePad or NotePad++

anything but Notepad.

Ralph

Post a Reply

Please log in to post a reply.

Did you know that one NerdKits customer controlled a laser pointer with his computer using a microcontroller? Learn more...