473,466 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

headers and linking confusion

6 New Member
I used to think that including header files gives me some extra function to use.
Like putting <stdlib.h> allows use of system("PAUSE") without doing anything special.

I disovered lately that it doesn't work that way with my own header files.
With main function in file A, extra functions in file B (both including header file C with declarations of functions in B) to use these functions in A I still need to link A and B - what raises the question: WHAT'S THE USE OF MY HEADER?
Help, please!
Mar 3 '08 #1
6 1783
gpraghuram
1,275 Recognized Expert Top Contributor
I used to think that including header files gives me some extra function to use.
Like putting <stdlib.h> allows use of system("PAUSE") without doing anything special.

I disovered lately that it doesn't work that way with my own header files.
With main function in file A, extra functions in file B (both including header file C with declarations of functions in B) to use these functions in A I still need to link A and B - what raises the question: WHAT'S THE USE OF MY HEADER?
Help, please!
For compilation the compiler needs to know the type of functions while it is compiling the code.
While linking it needs the original function for which you needs the body of the function.
Try to search in the net for difference between function definition and function declaration to get more info about this

Raghuram
Mar 3 '08 #2
przemek
6 New Member
Thank You,
to be more specific:

Using gcc -c source.c :
I can compile any code with undeclared functions and no header leading to declaration at all. I can link that output later to binaries with all needed definitions but again without any headers. So what is the point of writing header files (besides kind of documentation) ?

Using just gcc source.c :
I cannot compile code even with headers leading to declarations of functions which definitions are alredy compiled. I need to do all the linking I can't see any use for headers again.

My problem is not using but seeing the point of using separate .h declaration file?
Mar 3 '08 #3
gpraghuram
1,275 Recognized Expert Top Contributor
Thank You,
to be more specific:

Using gcc -c source.c :
I can compile any code with undeclared functions and no header leading to declaration at all. I can link that output later to binaries with all needed definitions but again without any headers. So what is the point of writing header files (besides kind of documentation) ?

Using just gcc source.c :
I cannot compile code even with headers leading to declarations of functions which definitions are alredy compiled. I need to do all the linking I can't see any use for headers again.

My problem is not using but seeing the point of using separate .h declaration file?
using gcc source.c means compiling and linking so you need to add the other .c file which has the declaration.
So the header file you have added will be used only during compilation and .c file is required during linking

Raghuram
Mar 3 '08 #4
przemek
6 New Member
using gcc source.c means compiling and linking so you need to add the other .c file which has the declaration.
If you mean -- using gcc source.c means compiling and linking so you need to add the other .c file which has the definitions (of everything declared in .h)
-- then I'm ok.
So the header file you have added will be used only during compilation and .c file is required during linking.

Raghuram.
Yes, and HOW is this header file used during compilation if it goes the same with it or witout it?
Please go a little deeper this path as I need undestanding more than confirmation of what I see.
Mar 3 '08 #5
Banfa
9,065 Recognized Expert Moderator Expert
The importance of the header file is adding robustness to your code and having the compiler pick up more errors for you.

By declaring the function in the header and then including this header in the c file in which the function is defined and the c files in which the function is called you cause the compiler to check that the function is being defined as per its declaration and being called as per the declaration. If it isn't in either case then compiler warnings (if not errors) will be produced.

By using a header to declare the API of you function(s) you ensure that the function is used in the same way where ever it is used and you use the compiler to check this for you making your programs more robust and easier to maintain.
Mar 3 '08 #6
przemek
6 New Member
It's all clear now, thank you!
Przemek
Mar 4 '08 #7

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

Similar topics

0
by: John Hunter | last post by:
I am using distutils on a darwin 10.3 OS X machine to install a python package which needs to find the tcl/tk headers to compile. On my system those headers are located at ...
20
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external...
13
by: tsoukase | last post by:
Hello, two questions: 1) Why is a library a collection of compiled source-files while each header-file is a single source? Would it be more efficient if they were both either compiled or not?...
3
by: bill | last post by:
I'm not sure if this belongs in comp.lang.c or in a compiler group, but I'm experiencing some confusion. Consider: $ cat a.c const char t={0xef, 0xbe, 0xad, 0xde}; const char *a=t; int...
1
by: Bonj | last post by:
Is incremental linking AND precompiled headers at the same time not like overkill? What is incrementaly linking if it's not the same as precompiled headers?
8
by: Abubakar | last post by:
Hi, I am writing some unmanaged c++ code that would need to be compiled in the future using compilers other than vc++. I'm using the feature of vc++ "use precompiled headers", is there going to...
10
by: Luke Meyers | last post by:
So, just a little while ago I had this flash of insight. It occurred to me that, while of course in general there are very good reasons for the conventional two-file header/implementation...
3
by: Robert McEuen | last post by:
Using A2K3, Windows XP I'm handling a many-to-many relationship with a linking table structure as follows (irrelevant fields omitted): tblIssue PK_IssueID (autonumber, primary key) IssueName...
9
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Recently, there was a Linux program distributed as source code, and it compiled fine on the majority of systems. However on some systems, it failed to compile. On some of these systems, people were...
38
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: ...
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,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...
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: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.