473,395 Members | 2,253 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,395 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 1429
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.