473,418 Members | 4,041 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

How to create header file with compiling

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
18 3548
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
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
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
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
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
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
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
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
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
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
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
Al
ok thx

Nov 22 '05 #13
Ian
Al wrote:
ok thx

To what? Please quote!

Ian
Nov 22 '05 #14

"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: A | last post by:
Hi, Can you actually compile header files? I'm writing a header file named "test.h" using Visual C++ 6.0 and I get the following error message when I try to compile it: "no compile tool...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
6
by: alan | last post by:
Dear all, I have written my own function by C. And my development platform is W2k with VC6.0. Then I also defined a header file to extern declare this function. After that, I include this...
3
by: Sujan Datta | last post by:
What are the possible effects of modifying an existing header file, which includes bunch of defines, function prototypes and some struct definitions. The structure of the header file looks...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
15
by: amit.man | last post by:
Hi, i have newbie qestion, when i write #include <somthing.h> the precompiler subtitue that line with the lines from "somthing.h" header file. when, where and how the compiler insert the...
7
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
4
by: mdh | last post by:
K&R briefly mention splitting programs into seperate files.(page 82) Using my compiler (X-code) I noticed that creating a header file gives, as expected one .h file, but creating a new ".c" file...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.