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

organizing mulitple source files

Hello,

If a program has two source files: main.c secondaryfunctions.c
and I have a header file: secondaryfunctions.h (with prototypes for the
function)

The only function in main.c is main() and it requires certain headers
(because it uses certain library functions). I added the headers
associated with the library functions at the top of main.c and I added
my custom header file for the secondary function.

The functions in secondaryfunctions.c also require the same library
functions as does main. Do I need to include the same headers in
secondaryfunctions.c? I have not done this because I figure when the
linker adds secondaryfunctions.c to the main program, the headers
within main.c will provide the prototypes to everything. My program
works as expected, but I'm not sure if it was an accident or whether my
line of thinking is accurate. So which is it?

Big thanks in advance :)

Nov 14 '05 #1
3 1337
G Patel wrote:
The functions in secondaryfunctions.c also require the same library
functions as does main. Do I need to include the same headers in
secondaryfunctions.c? I have not done this because I figure when the
linker adds secondaryfunctions.c to the main program, the headers
within main.c will provide the prototypes to everything. My program
works as expected, but I'm not sure if it was an accident or whether my
line of thinking is accurate. So which is it?


You should include the headers in the secondaryfunctions.c file also.
The prototypes from the includes are effectively pasted into the source
prior to compiling, so all the prototypes for the functions are present.
Try turning up the warning level on your compiler and it should warn
you of the implicit declarations.

--John
Nov 14 '05 #2
G Patel wrote:

Hello,

If a program has two source files: main.c secondaryfunctions.c
and I have a header file: secondaryfunctions.h (with prototypes for the
function)

The only function in main.c is main() and it requires certain headers
(because it uses certain library functions). I added the headers
associated with the library functions at the top of main.c and I added
my custom header file for the secondary function.

The functions in secondaryfunctions.c also require the same library
functions as does main. Do I need to include the same headers in
secondaryfunctions.c? I have not done this because I figure when the
linker adds secondaryfunctions.c to the main program, the headers
within main.c will provide the prototypes to everything. My program
works as expected, but I'm not sure if it was an accident or whether my
line of thinking is accurate. So which is it?

Big thanks in advance :)


Think in terms of modules. I will call your secondaryfunctions
thing the "sf" module for the duration of this reply.

Your sf module comprises two parts - the interface, and the
implementation.

The interface is in sf.h, and the implementation in (at the very least)
sf.c. The sf.h file should include all the interface information needed
by the compiler so that it can compile any source that needs to use
stuff from sf.c. It should also (#!)include any headers that it needs
for the compiler not to complain about it. One good way to achieve
this is to make #include "sf.h" the first line of sf.c, and then
compile. If the compiler moans about anything in sf.h, fix that
by including the necessary standard library header in sf.h, e.g.

#ifndef SF_H
#define SF_H 1

#include <stddef.h>

struct sf
{
int *p;
size_t size; /* this is why you need stddef.h */
};

/* etc */

#endif

Once your compiler's nag list is devoid of refs to sf.h, then
put any remaining #includes (that you need for shutting up the
compiler with regard to sf.c itself) below #include "sf.h"

sf.h is now "idempotent", which means you can stick it anywhere
in the list of includes in main.c.

Now do the same thing with main.c - i.e. add any further #includes
you need for that source.

That's how I do it, anyway (more or less), and it works for me.
Nov 14 '05 #3
On 8 Feb 2005 12:36:16 -0800, "G Patel" <ga********@gmail.com> wrote:
Hello,

If a program has two source files: main.c secondaryfunctions.c
and I have a header file: secondaryfunctions.h (with prototypes for the
function)

The only function in main.c is main() and it requires certain headers
(because it uses certain library functions). I added the headers
associated with the library functions at the top of main.c and I added
my custom header file for the secondary function.

The functions in secondaryfunctions.c also require the same library
functions as does main. Do I need to include the same headers in
secondaryfunctions.c? I have not done this because I figure when the
linker adds secondaryfunctions.c to the main program, the headers
within main.c will provide the prototypes to everything. My program
Here is where your reasoning jumped the track. It is usually true
that the linker will include one copy of a function regardless of how
many different modules/functions call that function. However, this
has nothing to do with headers.

Headers are use only during compile time, not during link time. The
prototypes, typedefs, struct declarations, and macros are needed at
compile time so the compiler will know how to deal with those portions
of your code that reference them.
works as expected, but I'm not sure if it was an accident or whether my
line of thinking is accurate. So which is it?


Really bad luck. Good luck would have resulted in an immediate
diagnostic of some kind so you would not mistakenly assume things were
ok.

<<Remove the del for email>>
Nov 14 '05 #4

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

Similar topics

1
by: Donald Firesmith | last post by:
We are converting the OPEN Process Framework Repository (www.donald-firesmith.com) of over 1,100 free open source reusable process components for building development methods for...
3
by: Jamie | last post by:
Hi, Thanks for the excellent answer to my last question! One more: Does anyone have a method they follow for organizing stylesheets themselves? They seem like they can get bloated and hard to...
6
by: BigMan | last post by:
I've come to the decision to use multiple namespaces for a very specific reason: I like to use descriptive names of UDTs and I sometimes come up with long UDT names (and file names too, since I...
10
by: Rada Chirkova | last post by:
Hi, at NC State University, my students and I are working on a project called "self-organizing databases," please see description below. I would like to use an open-source database system for...
0
by: netsurfer802 | last post by:
I have a question regarding working with seperate files and organizing these files. The company I'm working for is a small comuter company that calls people that have went to computer trade...
2
by: Sandor Palfy | last post by:
I have a VS solution with 3 c# class library projects and an "asp.net web application" project. The whole stuff is under integrated source control provided by VSS. The application is under...
14
by: bcap | last post by:
Hello, I really would apprciate help! =) What I want to do is be able to change the status of mulitple records using a drop down and a checkbox. I have a drop down called "ChangeStatus"...
6
by: awhan.iitk | last post by:
I have a set of variables that I want to share across mulitple c++ files. I was using the extern method so far. Is there any other way to do the same. The variables are not constants and I get the...
1
by: TheNewBoy | last post by:
Hi All. I am new to this forum... Don't know how to start but here it goes.... I am writing in VB.net a program with one form, yet mulitple textboxes. the data appears in each text box as...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...

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.