Page 1 of 1

Creating custom .h files and adding them to a project Project management Rate Topic: ***** 1 Votes

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Posted 06 August 2008 - 01:41 PM

*
POPULAR

Creating a Project in C/C++
Using various filetypes:
.h .cpp

This is a little IDE specific but I'll make it as general as possible.

This is quite specific, but it can be generalised. Basically, when you create a new project, you should already have a .cpp file added to it, since this is where the main loop goes. This file is often called main.cpp

Now, to add a new file, most IDEs will require you to go to File>New. You should then see an option called something like "Header file." If not, then it is most likely File>New>file...

Now, make sure that it is a .h file that you are creating, give it a name, and click next/OK. Follow the wizard until you are returned to the coding view, and you should now be working in a header file (WhateverYouCalledIt.h)

First off, I'm going to add some header guards. This is basically to prevent the file being included more than once, which can cause errors (saying that things have already been declared, and are now being redeclared)

The easiest one is a simple one-line tool. However, it's not used very often.
#pragma once

The more common method is to use #ifndef like so:
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED
// header content goes here
#endif
This looks more confusing than it is. let's break it down a little.

A header file is most often used to contain variable declarations, function prototypes, and class declarations. In this example, I'm just going to use a few variables, because you may not yet be at a level of writing functions/classes.

Here is a very basic header file:
#ifndef MYHEADER_H_INCLUDED
#define MYHEADER_H_INCLUDED

int a; // declare an int
double b; // declare a double
char c; // declare a char

// notice that I haven't assigned any values here?
// we do that next, in a cpp file

#endif
Now it's time to make a .cpp file to define everything in that header file. So, go to File>New>File... again. This time, however, instead of selecting .h we're selecting .cpp
Give it the same name as the header file. This will make it easier to manage later on. :)

Let's include our header file, and assign some stuff to our variables:
#include "myheader.h"

a = 4;
b = 20.2994;
c = 'D';
Pretty simple, huh? That's because we're not really learning any new code. All that we're doing is breaking up some code into manageable files. It seems a little pointless here, but it's much better to have a few files with a few hundred lines each than one big file with thousands of lines. It simply makes it easier to manage!

Now to write the code for our main.cpp
All we need to do is include our header file, and the contents of the cpp file will be included for you.
This is main.cpp:
#include <iostream> // input/output stream
#include "myheader.h" // use " " instead of < > so the compiler knows that it's a local header file

using namespace std;

int main ()
{
    cout << a << endl << b << endl << c;
    cin.get ();
    return EXIT_SUCCESS;
}

There are no scoping issues, since these variables have simply been dragged into the program at compile time.

Happy coding! :)

Is This A Good Question/Topic? 6
  • +

Replies To: Creating custom .h files and adding them to a project

#2 preeengles  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 28-March 09

Posted 28 March 2009 - 11:57 PM

uhmm...can you post the output of your code?

This post has been edited by preeengles: 28 March 2009 - 11:58 PM

Was This Post Helpful? 0
  • +
  • -

#3 khaotic  Icon User is offline

  • New D.I.C Head

Reputation: -4
  • View blog
  • Posts: 6
  • Joined: 27-June 09

Posted 11 December 2010 - 11:44 PM

Somewhat useful
Was This Post Helpful? 0
  • +
  • -

#4 youbob1212  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 132
  • Joined: 31-October 10

Posted 17 December 2010 - 04:27 PM

Wow thank you for writing this simple header tut. I never really understand why would someone use header files before reading this.

Now I'm going to start using header files on some of my large projects.

Also...

Can you post in this thread of an example of a header file that contains function/class declaration?

Thanks..
Was This Post Helpful? 1
  • +
  • -

#5 jvdubn  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 52
  • Joined: 08-June 11

Posted 30 June 2011 - 03:14 PM

http://www.dreaminco...ot-name-a-type/

Thought this was useful if anyone attempting this gets the error is received.
Was This Post Helpful? 1
  • +
  • -

#6 Deca  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-July 11

Posted 09 July 2011 - 02:32 PM

thanks i understand why to use headers now
Was This Post Helpful? 1
  • +
  • -

#7 oscode  Icon User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Posted 20 July 2011 - 07:50 AM

"A header file is most often used to contain variable declarations,", it's not. Also, you may cause undesirable effects by doing so, if you have two compilation units including this header file, each will have local storage for them. Modifying one won't affect the other. It's most common to declare global data in a source file and declare its extern counterpart in the header file, that way multiple compilation units will access the same data.
Was This Post Helpful? 1
  • +
  • -

#8 flypro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 07-November 11

Posted 07 November 2011 - 04:17 PM

Excellent thanks i understand them better know :)
Was This Post Helpful? 0
  • +
  • -

#9 cseNoob  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 09-April 11

Posted 12 November 2011 - 10:03 AM

Great job!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1