473,241 Members | 1,619 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,241 software developers and data experts.

problems with .h files

can someone explain why
int k; canNOT be definied in .h file?

what are general rules for what can/cannot be placed in .h file. FAQ
doesn't seem to address this problem.

Jul 23 '05 #1
10 1427
See Lakos ;-)

Jul 23 '05 #2

davidru...@warpmail.net wrote:
See Lakos ;-)


I have been interviewed by him not so long ago.. i.e. forgot to ask
this question. but he is probably expensive for private consultations,
I would assume!

suggest something more within the scope of this group or ballpark me
through an explanation.
thanks

Jul 23 '05 #3
puzzlecracker wrote:
can someone explain why

int k; canNOT be definied in .h file?
The purpose of header files is to be '#include'd into several
translation units. If you place a definition of an object with external
linkage in header file, you'll sooner or later end up with several
definitions of this object in the program. This is a violation of ODR
(One Definition Rule) normally resulting in compilation error (at
linking stage). That's why such definition cannot be placed into header
file.
what are general rules for what can/cannot be placed in .h file.


H-files are for non-defining declarations and for certain kinds of
definitions. Namely, class definitions, template definitions, inline
function definitions, typename definitions can/should be placed into
header files. Entities with static linkage can also be defined in header
files, if for some reason you need to do so. Everything else (unless I
missed something) should only be _declared_ (as opposed to _defined_) in
header files.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #4

Andrey Tarasevich wrote:
puzzlecracker wrote:
can someone explain why

int k; canNOT be definied in .h file?
The purpose of header files is to be '#include'd into several
translation units. If you place a definition of an object with

external linkage in header file, you'll sooner or later end up with several
definitions of this object in the program. This is a violation of ODR
(One Definition Rule) normally resulting in compilation error (at
linking stage). That's why such definition cannot be placed into header file.
what are general rules for what can/cannot be placed in .h file.
H-files are for non-defining declarations and for certain kinds of
definitions. Namely, class definitions, template definitions, inline
function definitions, typename definitions can/should be placed into
header files. Entities with static linkage can also be defined in

header files, if for some reason you need to do so. Everything else (unless I missed something) should only be _declared_ (as opposed to _defined_) in header files.

--
Best regards,
Andrey Tarasevich

3 questions in this regards:

a. if I define an object in .h but it is [.h file] only included in
just one file with no name conflicts, then it won't cause problems with
external linkage, but compiler still complains, why?

b. Why is inline function bypassing it? Actually b/c it is static in a
nature, but it is not discouraged as other static functions, objects,
etc., for bloating the space... why? also, sometimes, compiler chooses
not to inline, would it then affects the linkage?
c. what is the deal with 'const' definition in .h files... what is the
linkage rules applied to them?
THANKS THANKS THANKS

Jul 23 '05 #5

puzzlecracker wrote:
can someone explain why

int k; canNOT be definied in .h file?

It can.
what are general rules for what can/cannot be placed in .h file. FAQ
doesn't seem to address this problem.


Because it's not a problem. There are no rules.
You can write all you program in a .h file and then include it in a
..cpp file and include your .cpp file in .zzz file and compile it.
Everything's allowed. But it would be ridiculous.
P.Krumins

Jul 23 '05 #6

Peteris Krumins wrote:
puzzlecracker wrote:
can someone explain why

int k; canNOT be definied in .h file?


It can.
what are general rules for what can/cannot be placed in .h file. FAQ doesn't seem to address this problem.


Because it's not a problem. There are no rules.
You can write all you program in a .h file and then include it in a
.cpp file and include your .cpp file in .zzz file and compile it.
Everything's allowed. But it would be ridiculous.
P.Krumins


I know it is! even confirmed it. I am curious what is the
justification for such standard.

thanks

Jul 23 '05 #7
puzzlecracker wrote:

3 questions in this regards:

a. if I define an object in .h but it is [.h file] only included in
just one file with no name conflicts, then it won't cause problems with
external linkage, but compiler still complains, why?
No, it shouldn't cause problems. What kind of complaint are you
receiving from the compiler?
b. Why is inline function bypassing it?
Because that's the way it is in C++.
Actually b/c it is static in a nature,
No, it is not static. Inline functions have external linkage by default.
Even though each translation unit gets its own definition of an inline
function, the address of the function should be the same in all
translation units.
but it is not discouraged as other static functions, objects,
etc., for bloating the space... why?
I don't exactly understand this question.
also, sometimes, compiler chooses
not to inline, would it then affects the linkage?
No. "Linkage" in C++ is a language level concept. It is not affected by
low-level compiler decisions.
c. what is the deal with 'const' definition in .h files... what is the
linkage rules applied to them?


In C++ definitions of const-qualified objects have static linkage by
default. The rest follows.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #8
puzzlecracker wrote:
Can someone explain why

int k;

cannot be defined in a *.h [header] file?
It certainly can.
what are general rules
for what can/cannot be placed in a *.h [header] file.
Header files are simply files that are included
near the top [head] of a *.cc [source] file or another header file.
The *.h extension is merely a convention.
Files with the *.h extension or any other extension
including no extension may be included anywhere if a fail
but probably should *not* be called header files
unless they are included somewhere near the head of the file.

There are no rules for what an/cannot be placed in a header file
except when the header file is used to define a *module interface*
which is the most common use for a header file.
Like all other interfaces, a module interface has *no* substance.
It should not, by itself, cause the compiler to emit code
or reserve memory for data. It should not, for example,
contain definitions like:

int k;

which would compel the compiler to reserve memory for k.
It may contain type definitions and declarations like:

extern int k;
void f(void);
inline
int g(int i) {
return i + 1;
}
class X {
private:
int I;
public:
X(int i = 0): I(i) { }
};

etc.

A module interface (header file) should be idempotent.
The contents should be enclosed in "guard macros:
cat file.h #ifndef GUARD_FILE_H
#define GUARD_FILE_H 1
// contents
#endif//GUARD_FILE_H

The module definition (header file) should be self-sufficient.
It should include every module interface (header file)
required to process it's contents.
[The C++] FAQ doesn't seem to address this problem.


The problem is that neither C or C++ possess a strong notion of modules
much less module interfaces like modula. See Modula-2

http://en.wikipedia.org/wiki/Modula-2

C and C++ programmers are obliged
to manufacture ad hoc module interfaces
and the most convenient way to do that is using header files.
Jul 23 '05 #9

No, it is not static. Inline functions have external linkage by default. Even though each translation unit gets its own definition of an inline function, the address of the function should be the same in all
translation units.


wait, if inline function has external linkage, wouldn't that cause a
link problem when units get assambled? or it is an exception to the
general rule of external linkage?

Jul 23 '05 #10
puzzlecracker wrote:
No, it is not static. Inline functions have external linkage by

default.
Even though each translation unit gets its own definition of an

inline
function, the address of the function should be the same in all
translation units.


wait, if inline function has external linkage, wouldn't that cause a
link problem when units get assambled? or it is an exception to the
general rule of external linkage?


Yes, you can put it this way, I guess. It is an extension to the general
rule of "external linkage", as it is known in outside the scope of C++
language, in general linker world :)

Inside C++ language though, it is not an extension. It is just the way
the ODR is formulated in language specification. Inline functions shall
have external linkage by default and this shall not cause any problems.
How implementations deal with this requirement is their own internal
business (read about comdat sections in object files for details).

--
Best regards,
Andrey Tarasevich

Jul 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: John Caruthers | last post by:
HELP! This problem is driving me crazy. All other web sites that are being served out through this server are working, including sites and virtual directories that are under the Default Web...
15
by: Rob Ratcliff | last post by:
I'm compiling the latest version of a CORBA ORB called MICO on a Cray X1. It makes heavy use of templates and namespaces. Up until the link step, the C++ source code compiled flawlessly. But, when...
9
by: Daniel Moree | last post by:
I'm using MS VC++ 6.0 I'm working on a big project. I've currently have several files for this project. Here's the problem. I have one header file phead.h I have two code files main.cpp and...
2
by: Erik | last post by:
Hi Everyone, I'm having real problems compiling some source for eVC4++. The errors I am getting are below: It all seems to be centred around winsock. If I move the afsock.h reference to before...
21
by: matvdl | last post by:
I have a system that was originally developed in asp - the pages are saved in SQL (there are over 10,000 pages) and saved to a temp directory in the server when requested by a client. I have...
2
by: Jack Fox | last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0 applications: In each of 3 production environments we maintain a Windows Server 2003 machine running NTFS as a file server....
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
2
by: subsanta | last post by:
My computer has so many problems and ive looked around on the internet and ive managed to fix some of them. I know that i have a few viruses on my computer, but i cant get rid of them, in one case i...
3
by: =?Utf-8?B?VG9kZCBEb2JtZXllcg==?= | last post by:
I am working on developing a program using Visual Studio 2003 but am having problems getting my program to find my GL.h and GLU.h, and I am guessing it will have the same problems trying to link to...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.