The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How do you create a header file?
Discuss How do you create a header file? in the C Programming forum on Dev Shed. How do you create a header file? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 4th, 2013, 05:33 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 17
Time spent in forums: 3 h 31 m 51 sec
Reputation Power: 0
|
|
|
How do you create a header file?
(In Visual Studio 2010)
I would click Add new item, Header File, name it, and it would place in Header files. But when I write out code for it, save it, and then call it in my main file it would say "Cannot open source file "Cclasses.h"
I don't understand what I'm doing wrong. Where am I supposed to place this?
|

March 4th, 2013, 05:36 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 3
Time spent in forums: 1 h 18 m 56 sec
Reputation Power: 0
|
|
|
Please show me the code for the header.
|

March 4th, 2013, 05:45 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 17
Time spent in forums: 3 h 31 m 51 sec
Reputation Power: 0
|
|
Sure thing!
Code:
int add(int i,int ii)
{
return i + ii;
}
I just did a simple adding function to make sure it'll work.
|

March 4th, 2013, 05:53 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 17
Time spent in forums: 3 h 31 m 51 sec
Reputation Power: 0
|
|
|
Wait, nevermind. I found out how to make it work. Thanks for the help guys!
|

March 4th, 2013, 08:37 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Excuse me, but did you put live code in a header file? Why? Never do that! Code belongs in source files, not in header files!
The purpose of header files is to share common macros (#define's) and data type definitions (including struct declarations and typedefs) among source files in your project, as well as to notify all interested source files, via function prototypes and extern declarations, of functions and global variables that exist in one source file but intended to be used by other source files. Therefore, you only put information about functions and variables in a header file, not the actual functions and global variables themselves.
Why is that? Take your misbegotten header file containing the actual function, add(), and #include it in two source files in your project. When you try to build that project, you will get a linker error because there will exist two different add functions by the same name -- that is a name collision. Rather, place in one and only one source file the actual function and then in the header file you place the function prototype for add:
int add(int i,int ii);
Please note the semicolon, which is what makes that a function prototype. That function prototype can be #include'd in as many source files as pleases you. What it tells the compiler for each source file is that somewhere there exists a function by this description and that the linker will fill in its actual address later.
Some misguided teachers and authors, in an attempt to avoid teaching you how to properly create a multiple file project, suggest the quick-and-dirty approach of #including files containing live code into one main source file. Avoid them like the plague! Learn to use either your development environment's project manager (Visual Studio has one) or makefiles.
When you move on to C++, you will see some apparent violations of the rule to not place code into header files; eg, inline code, templates. But those are specific features that do not violate the rule.
Use header files as they were intended to be used.
|

March 5th, 2013, 06:39 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by Apathy Please show me the code for the header. |
Why? The content of a file has no bearing on its acessability within a file system  .
|

March 5th, 2013, 06:42 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: UK
|
|
Quote: | Originally Posted by miz656
Code:
int add(int i,int ii)
{
return i + ii;
}
|
That is not what header files are for. Header files should contain declarative code that is defined elsewhere in separate compilation units.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|