Solved

Header Files

Posted on 1998-08-21
9
216 Views
Last Modified: 2010-04-15
I am experienced in C and C++ but I am not veery experienced in making Header files and accesing them from a program,
so I was hoping that someone could give me an example on how I could do something like that!

Thanks

Anton
0
Comment
Question by:smoothasice
9 Comments
 
LVL 4

Expert Comment

by:sganta
ID: 1252254
Hello !

This is not very difficult.

For eg:

Take a COMPILER project.

It is very hard to understand, if you have in the SINGLE FILE.

Now what I will do is , I will divide into different files.

1. Definition file
    Declaring Global Variables,

   <Def.h>

    Here you declare all the definitions.

2. Lexical Analyzer

    LEX.C

    #include "def.h"   /* This is your definition file */

   -- do whatever you want.

   It will include the code of def.h

I hope this helps you
   
0
 

Author Comment

by:smoothasice
ID: 1252255
what he said didn't really hellp me out much
0
 
LVL 1

Accepted Solution

by:
nilos earned 100 total points
ID: 1252256
The basic idea behind header files is to reuse definitions or even many includes to other headers files.

Let's say that you have a program composed by two modules and booth modules uses the same struct definition.

a) Create a file, let's say BaseProg.H and insert:

1. All includes that the two modules uses.
2. The struct definition in there.

b) In the two modules, remove de common includes
c) Add a line #include "BaseProg.H"

d) Now, every time that you change the struct definition (I'm assuming that you uses some kind of makefile or, better, a kind of integraded environment, the two modules recompiles and Bingo!

If you need a working sample I can supply one.

Last thing

Notice that i wrote #include "BaseProg.h". You will see that the compiler includes uses something like #include <stdio.h>. The difference in the constructions above indicates that the first searches for the included file in the current directory. The other looks in the INCLUDE= directory, normaly the compiler include directory.


0
 
LVL 11

Expert Comment

by:alexo
ID: 1252257
Anton, it is hard to understand what you need from the wording of the question.
Do you mean .H files usually accessed by the #include directive?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Expert Comment

by:newexpert
ID: 1252258
How to create/manage header files:

Things that can go into header files:

1.  Reusable definitions like
other header files: #include <stdio.h>
constants: #define CONSTANT 8
function prototypes: void print(void);
macros: #define debugmacro(x) printf("Debugging ... %s", #x); x;
class definitions: class dummy { int i; public int access(); };
extern global variables: extern int globalint;

2.  Conditional compilation statements:
eg. different code for different compilers:
#ifdef __STDC__
/* Some defintions here. eg ANSI C function prototypes. */
#else
/* Some other definitions here. eg K&R function prototypes. */
#endif

eg. different target requirement:
#define DEBUGVERSION
/* ... */
#ifdef DEBUGVERSION
/* Include debug macros or functions here */
#else
/* Include no debug functions. */
#endif

3.  Customized error messages for your own project:
#ifndef MYCONSTANT
#error Should have declared MYCONSTANT by now!!
#endif

4.  Customized features for your compiler:
#pragma /* something your compiler can understand. */

Note: Sometimes header files may not be included more than once.  This is achieved by using include guard:

#ifndef __THISHEADERFILE

#define __THISHEADERFILE

/* all header material goes here. */

#else

/* put nothing here. */

#endif

Now if you include the header twice, then the 1st #include includes content of file but 2nd #include includes nothing.
0
 
LVL 1

Expert Comment

by:nilos
ID: 1252259
NewExpert made a point. Like we said here in Brazil "he kiled the snake and showed the stick".

0
 
LVL 11

Expert Comment

by:alexo
ID: 1252260
Seems like the wrong point to me.  NewExpert gave a summary of preprocessor features.  However, most of them can be used just as well in source files.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1252261
Well I would have said headers are defined by wanting to share common stuff - but also but by what they shouldn't include:

Example: Don't declare storage, do declare definitions */

e.g.
int x ; /* Don't put this is a .h */
extern int x ; /* Do put this in a .h */
0
 
LVL 1

Expert Comment

by:nilos
ID: 1252262
Elaborating Answer2000 one way to do this is:

1. Put this in every header file:

#ifdef      MAIN
#define      EE
#else
#define      EE      extern
#endif

2. To define a variable use:

EE int somevar;

3. In the module that contain main/winmain, before all includes, do:

#define MAIN

4. Restrictions. The variables defined as above cannot be initialized in the header.


0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Is string constant address ? 10 189
C#, VS15, StructLayout 1 116
Can case within switch statement specify range of values ? 3 71
Line meaning 9 77
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now