473,465 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Including header file

1,059 Top Contributor
Hello everybody,

I am having a problem with including header file. The problem is stated below:

file a.h
Expand|Select|Wrap|Line Numbers
  1. struct __solid
  2. {
  3.  char s[100];
  4. }solid;
  5.  
  6. bool initsolid();
  7.  
  8.  
file a.c
Expand|Select|Wrap|Line Numbers
  1. bool initsolid()
  2. {
  3.  solid.s="johny";
  4. }
file main.c
#include "a.h"
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.  initsolid();
  4. }
  5.  
  6.  
The problem with the above three file is while i compile its okay. but when i try to link(create exe) it failed. The caused error is solid is already defined in a.obj. and linking failed. I also tried this in a.h

file a.h
Expand|Select|Wrap|Line Numbers
  1. #ifndef a_h
  2. #define a_h
  3. struct __solid
  4. {
  5.  char s[100];
  6. }solid;
  7.  
  8. bool initsolid();
  9. #endif
  10.  
It was not helpful either.
Advance thanks for the solution..

regards,
johny
Jan 10 '10 #1

✓ answered by Banfa

The included protection used in your final example is to prevent you including a header file twice into a single C file. It wont help across multiple C files.

Your problem is that header files should really only contain declarations, declarations

define data types
declare the existence of variables
declare the existence of functions

However your header file includes a definition, a definition

Provides the body for a function
Causes the compiler to actually allocate storage space for a variable.

Your statement
Expand|Select|Wrap|Line Numbers
  1. struct __solid
  2. {
  3.   char s[100];
  4. } solid;
Declares the variable type struct __solid but defines the variable solid. Since you include the header into 2 C files the variable is defined in each of them and when you link you get a symbol defined multiple times error because the linker is unable to know which of the 2 definitions of solid should be used for the program.

You should not be defining data in header files. what you need to do is this, in the header use the statements

Expand|Select|Wrap|Line Numbers
  1. struct __solid
  2. {
  3.   char s[100];
  4. };
  5.  
  6. extern struct __solid solid;
  7.  
The statement on line 1 declares the type struct __solid. The statement on line 6 declares (not does not define) the variable solid. The declaration tells the compiler that the symbol exists somewhere and that it should not create it.

Then in one of your C files include
Expand|Select|Wrap|Line Numbers
  1. struct __solid solid;
This actually defines the symbol(variable) solid, but specifically limits the definition to a single C fgile so the symbol is only defined once across all files. The the declaration in the header lets all C files access the symbol.


A few notes on best practice
  • It is considered bad form to have global data that just anyone can access. It causes maintenance issues and is a major contributor to bugs. If you need persistent data declared at the top of a file then provide access to it via functions rather than just let anyone access it.
  • _ at the start of a symbol name, as you have done with __solid is reserved for platform internal types, variables and functions and should be avoided. It is a common convention to add an _t to the end of type names as in solid_t or in our company we have the convention of type names starting with a capital letter Solid.

2 4513
Banfa
9,065 Recognized Expert Moderator Expert
The included protection used in your final example is to prevent you including a header file twice into a single C file. It wont help across multiple C files.

Your problem is that header files should really only contain declarations, declarations

define data types
declare the existence of variables
declare the existence of functions

However your header file includes a definition, a definition

Provides the body for a function
Causes the compiler to actually allocate storage space for a variable.

Your statement
Expand|Select|Wrap|Line Numbers
  1. struct __solid
  2. {
  3.   char s[100];
  4. } solid;
Declares the variable type struct __solid but defines the variable solid. Since you include the header into 2 C files the variable is defined in each of them and when you link you get a symbol defined multiple times error because the linker is unable to know which of the 2 definitions of solid should be used for the program.

You should not be defining data in header files. what you need to do is this, in the header use the statements

Expand|Select|Wrap|Line Numbers
  1. struct __solid
  2. {
  3.   char s[100];
  4. };
  5.  
  6. extern struct __solid solid;
  7.  
The statement on line 1 declares the type struct __solid. The statement on line 6 declares (not does not define) the variable solid. The declaration tells the compiler that the symbol exists somewhere and that it should not create it.

Then in one of your C files include
Expand|Select|Wrap|Line Numbers
  1. struct __solid solid;
This actually defines the symbol(variable) solid, but specifically limits the definition to a single C fgile so the symbol is only defined once across all files. The the declaration in the header lets all C files access the symbol.


A few notes on best practice
  • It is considered bad form to have global data that just anyone can access. It causes maintenance issues and is a major contributor to bugs. If you need persistent data declared at the top of a file then provide access to it via functions rather than just let anyone access it.
  • _ at the start of a symbol name, as you have done with __solid is reserved for platform internal types, variables and functions and should be avoided. It is a common convention to add an _t to the end of type names as in solid_t or in our company we have the convention of type names starting with a capital letter Solid.
Jan 10 '10 #2
johny10151981
1,059 Top Contributor
I really appreciate your answer. Thanks for it. I already found this site very helpful. Thanks again...

regards
Johny
Jan 10 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jim Mesara | last post by:
I have the following file structure: / header.asp test.asp /subDir testpage.asp /images I would like to have header.asp to be usable from all directories.
11
by: cppaddict | last post by:
Say that your CustomClass.h header files requires #include <string> Now say that your CustomClass.cpp file also requires string. Is it good form to repeat the <string> include to make the...
3
by: Miguel | last post by:
I have a header file that I want to share among various files, and I use: #ifndef UTIL_H_ #define UTIL_H_ file contents #endif
8
by: nrhayyal | last post by:
Hi c++ Gurus, Need your blessing. while testing few aspects with respect to header file inclusions, i observed few things which i would like to share with you. i have a file sqlca.h in which a...
6
by: Al-Burak | last post by:
I have a class which only purpose is to provide services to a variety of classes in other files. The 'manipulator' class is aware of the other classes only because the header files have been...
4
by: 'Mani | last post by:
Hi, This is just a generic question, where i want to know what is the difference in including a header file in a .h file and .cpp file. I have a class called MyClass (MyClass.h & MyClass.cpp)....
11
by: Gary Wessle | last post by:
Hi is it right to have a line like #include <path/to/header.hfor a library on my system, in my header file and use some functions provided by this library in the implementation file (file.cpp)...
1
by: Martin Mücke | last post by:
I got a website consisting of about 150 php pages. The site uses a frameless table based design. Header and menu are always the same and therefore should be extracted. At the moment I got a...
8
by: nguillot | last post by:
Hello. If I have the following classes: class B {}; typedef B tB; if A is: class A
3
by: KIRAN | last post by:
Hello all, My question is about the way of including header files(*.h) in source files (*.c) I have three folders, -build ( for project makefiles) -include ( for *.h files) -src (for *.c...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.