By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,919 Members | 1,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,919 IT Pros & Developers. It's quick & easy.

How to create header file with compiling

P: n/a
Al
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
....to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?

Could you tell me more infos of what the preprocessor does and can do?

Nov 22 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Hi,

It should work unless...
some header you include has the same MYLIST_H defined. Try to take a more
random name.

#includes can go anywhere (of course before you use anything from them) but
it makes sense to put them after the #ifndef to save some loading time (the
preprocessor would load the files and everytime your header files is
included somewhere.

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Al" <al************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
...to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?

Could you tell me more infos of what the preprocessor does and can do?

Nov 22 '05 #2

P: n/a
Al
Could it depend on the compiler? I use Dev-C++.

Moonlit wrote:
Hi,

It should work unless...
some header you include has the same MYLIST_H defined. Try to take a more
random name.

#includes can go anywhere (of course before you use anything from them) but
it makes sense to put them after the #ifndef to save some loading time (the
preprocessor would load the files and everytime your header files is
included somewhere.


Nov 22 '05 #3

P: n/a
Al wrote:
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
...to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?

Could you tell me more infos of what the preprocessor does and can do?


This pattern is often used in header files (.h). It serves no purpose
in a .cpp file.

A header file is something that is created by you, not by compiling.
Its purpose is to declare the types for symbols. Other cpp files can
#include the header file to share the declarations.

--
Scott McPhillips [VC++ MVP]

Nov 22 '05 #4

P: n/a
On 19 Nov 2005 08:16:46 -0800, "Al" <al************@gmail.com> wrote:
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
...to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?
Header files are not generated automatically by the compiler. You need
to write them yourself!

What the above does is called an "inclusion guard". You put all of the
other things which belong in the header file between "#define
MYLIST_H" and "#endif". The reason for this is that different header
files included by one source file can include other headers (either
directly or indirectly, by being included by other headers), and
sometimes they are the same files. To prevent multiple declarations
from happening, the compiler will define the symbol MYLIST_H the very
first time that such a header is read and for all subsequent include
directives of that file for the same translation unit (e.g. your .cpp
file), it sees that MYLIST_H has been defined and can safely skip over
that header.

Unfortunately, there are some compilers including MSVC++ which do not
honor the include guards ... you need to write:

#pragma once

to achieve the same effect. Since I usually need to have my code work
with different compilers, I always have something like this in place
of the simpler include guard:

#ifndef Assignment_H_INCLUDED
#define Assignment_H_INCLUDED

// _MSC_VER is always defined by the Microsoft C++ compiler:
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1020)
// ... rest of header file ...
#endif // Assignment_H_INCLUDED
Could you tell me more infos of what the preprocessor does and can do?


I think this will help:

http://www.parashift.com/c++-faq-lite/

Better still, get a good beginner's textbook on how to program in C++
and work through all the examples.

--
Bob Hairgrove
No**********@Home.com
Nov 22 '05 #5

P: n/a
On Sat, 19 Nov 2005 18:07:41 +0100, Bob Hairgrove
<in*****@bigfoot.com> wrote:
Unfortunately, there are some compilers including MSVC++ which do not
honor the include guards ... you need to write:


That's not true. MSVC++ provides the #pragma once as an extra, which
is cleaner - but the standard include guards work just the same.
Nov 22 '05 #6

P: n/a
Al
Do you mean I just need to change the extension for using the header?
That's better then...
What is #pragma once and usually #pragma for? The FAQ doesn't tell
about it

Nov 22 '05 #7

P: n/a
On 19 Nov 2005 09:24:08 -0800, "Al" <al************@gmail.com> wrote:
Do you mean I just need to change the extension for using the header?
That's better then...
Headers usually have an extension of *.h or *.hpp, but there may be
others. Typically, only declarations of things (classes, templates,
functions, etc.) are kept in header files. Definitions (i.e. function
bodies, static member data initializers, etc.) are kept in *.cpp
files. This is a fundamental difference (declaration vs. definition)
which you need to know, and I'm sure that the FAQ has something to say
about it.

The big exception to this are template functions and classes: the
definitions (i.e. bodies) of these must be either in the same header
file where the declarations are, or else kept in a separate file but
included at the bottom of the header. If you like to keep the template
function bodies in a separate file, some people like to give it the
extension *.cc to keep it separate from *.cpp files. That way, the
compiler won't try to compile it as a separate translation unit if it
finds the file in the same directory as the *.cpp files. There is also
a new C++ feature cncerning templates using the keyword "export", but
almost nobody uses it yet, so I wouldn't worry about that right now
(you have enough other things to worry about! ;)
What is #pragma once and usually #pragma for? The FAQ doesn't tell
about it


Well, I already told you what "#pragma once" is for ... just read my
last message again a little more carefully. If you don't use the
Microsoft compiler, you won't need it. But maybe you will want to use
it some day in the future, then you will know what it is about.

In general, #pragma directives are implementation-specific things that
are usually not portable from one compiler to the next. Some #pragmas
are the same between some different compilers (not all) by
coincidence, or perhaps by market strategy, but they don't have to be.
To find out what any specific #pragma directive means, you have to
consult the documentation included with your particular compiler.
Since the C++ standard doesn't have much (if anything) to say about
them, that's probably why they aren't discussed in the FAQ. AFAIK the
standard only requires conforming implementations to ignore unknown
#pragma directives instead of issuing an error. Some compilers will
warn about an unknown #pragma, though.

--
Bob Hairgrove
No**********@Home.com
Nov 22 '05 #8

P: n/a
Al wrote:
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
...to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?

Could you tell me more infos of what the preprocessor does and can do?


It would help if you showed actual code since your question is a bit
confusing. If the #ifdef MYLIST_H stuff generally belongs in the header
file only, not in the .cpp file. If you put it in both it will likely
cause problems.
Nov 22 '05 #9

P: n/a
Al
Now it's all right:when I type the code in dev c++, on a new source, it
is a *.cpp file, so I just renamed it to *.h. But I don't understand
what are the #ifndef... for.
Shall I better put the implementation in an other file or keep it in
the header? Bob Hairgrove said that for templates I can leave them in
the header.
The code is:

#ifndef MYLIST_H
#define MYLIST_H

#include <iostream>
using namespace std;

template<class T>
class List;

template<class T>
class Link
{
friend class List<T>;
public:
Link(T d, Link<T>* n): data(d), next(n) {}
private:
T data;
Link<T>* next;
};

template<class T>
class List
{
public:
List(): first(0) {}
~List();
void insert(T t);
void print();
private:
Link<T>* first;
Link<T>* newNode(T t, Link<T>* p)
{ Link<T>* q = new Link<T>(t, p); return q;}
};

template<class T>
List<T>::~List()
{
for(Link<T>* p=first;p;)
{ Link<T>* temp = p;
p = p->next;
delete temp; }
}

template<class T>
void List<T>::insert(T t)
{
Link<T>* p = new Link<T>(t, first);
first = p;
}

template<class T>
void List<T>::print()
{
for(Link<T>* p = first; p; p = p->next)
cout <<p->data <<"->";
cout <<"*\n";
}

#endif

Nov 22 '05 #10

P: n/a
Al wrote:
Now it's all right:when I type the code in dev c++, on a new source, it
is a *.cpp file, so I just renamed it to *.h. But I don't understand
what are the #ifndef... for.
Shall I better put the implementation in an other file or keep it in
the header? Bob Hairgrove said that for templates I can leave them in
the header.
The code is:

#ifndef MYLIST_H
#define MYLIST_H
...
...
#endif


// somefile.cpp
#include "mylist.h"
#include "someotherfile.h"

During the compilation of somefile.cpp, the first include will compile
your mylist.h file, which is good. The second include will compile some
other file. But what happens if someotherfile.h also #include's
mylist.h?? That would cause "symbol is already defined" errors.

The #ifndef... pattern prevents compiling your file twice in a case
where it is included twice by a cpp file.

--
Scott McPhillips [VC++ MVP]

Nov 22 '05 #11

P: n/a
Ian
Al wrote:
Now it's all right:when I type the code in dev c++, on a new source, it
is a *.cpp file, so I just renamed it to *.h. But I don't understand
what are the #ifndef... for. You then have to include the .h file in a .cpp file for the compiler to
compile.

main.cpp:

#include "x.h"

int main()
{
....
}
Shall I better put the implementation in an other file or keep it in
the header? Bob Hairgrove said that for templates I can leave them in
the header.
That depends on your compiler. Some will look for template bodies,
typically in a file with the same name as your header, but with a .cpp
or .cc extension.
The code is:

#ifndef MYLIST_H
#define MYLIST_H

#include <iostream>
using namespace std;

Don't do this in header files!

Only put using namespace xxx in your cpp files, otherwise it will apply
to all files that include your header.

Ian
Nov 22 '05 #12

P: n/a
Al
ok thx

Nov 22 '05 #13

P: n/a
Ian
Al wrote:
ok thx

To what? Please quote!

Ian
Nov 22 '05 #14

P: n/a

"Al" <al************@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I'm still trying to do this but it never worked!
In a .cpp file, I write the code, and at the beginning, I write:
#ifndef MYLIST_H
#define MYLIST_H
...to end:
#endif

What's wrong with it for creating a header file when compiling?
Do I need to write in the block the #include (s)?

Could you tell me more infos of what the preprocessor does and can do?


It's supposed to go in the .h file, not the .cpp file.

Say your header file is mylist.h:
#ifndef MYLIST_H
#define MYLIST_H
.... to end
#endif

Then in your .cpp you just do:
#include "mylist.h"

Then it should be fine.
Nov 22 '05 #15

P: n/a
In article <11*********************@g43g2000cwa.googlegroups. com>,
Al <al************@gmail.com> wrote:
Shall I better put the implementation in an other file or keep it in
the header?


For questions such as this, and similar ones, see Chapter 9
of Stroustrup's The C++ Programming Language (3rd or special edition).
As a bonus, you get everything else said in the text!
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #16

P: n/a
Al

Greg Comeau wrote:
For questions such as this, and similar ones, see Chapter 9
of Stroustrup's The C++ Programming Language (3rd or special edition).
As a bonus, you get everything else said in the text!


I just ordered it on amazon and I'll have it for christmas. I knew I
needed it, so it's the moment to start it.

Nov 22 '05 #17

P: n/a
Al

Jim Langston wrote:
It's supposed to go in the .h file, not the .cpp file.

Say your header file is mylist.h:
#ifndef MYLIST_H
#define MYLIST_H
... to end
#endif

Then in your .cpp you just do:
#include "mylist.h"

Then it should be fine.


Thanks, I knew that but I thought it was for compiling a *.cpp file and
making of it a *.h file, because my compiler doesn't create new header
files. So I just write it in a source *.cpp and rename it to .h and it
works.

Nov 22 '05 #18

P: n/a
In article <11*********************@g14g2000cwa.googlegroups. com>,
Al <al************@gmail.com> wrote:
Greg Comeau wrote:
For questions such as this, and similar ones, see Chapter 9
of Stroustrup's The C++ Programming Language (3rd or special edition).
As a bonus, you get everything else said in the text!


I just ordered it on amazon and I'll have it for christmas. I knew I
needed it, so it's the moment to start it.


Good for you. Remember to read through it each new year!
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.