473,569 Members | 2,768 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using #ifndef

I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
....
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?

Thanks!

John

Nov 14 '05 #1
25 4881
In article <10************ ***@proxy2.srv. ualberta.ca>,
John Hanley <jd******@telus planet.net> wrote:
Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.


What's *in* the .h file? It should just be function prototypes, not
complete function definitions.

-- Richard
Nov 14 '05 #2
>I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.


You put the functions in program.h? That's why. Header files are
generally used for function PROTOTYPES, type definitions, #defines,
and sometimes global variables (with "extern"). Defining a function
in a header file, then including it in more than one file which
will be linked together into the same program, is a problem. As
you observed, you get multiple definitions of functions.

#ifndef only works within the same compilation unit. For example,
it WILL protect you from including "program.h" twice, or from
including "program.h" and "foo.h" which includes "program.h" . It
will NOT protect you from "program.h" already being included in
another compilation unit. The compiler is not psychic. It does
not know that you are going to link that compilation unit you already
did and the current one together into the same program. And even if
it was, that's not how #ifndef is supposed to work. You start with
a clean slate at the start of every compilation unit.

Gordon L. Burditt

Nov 14 '05 #3
"John Hanley" <jd******@telus planet.net> writes:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here?


Yes, you've got a typo on line 137 of program.c.

Seriously, I don't see anything wrong in what you've posted. Try
narrowing your program down to a set of very small files (say 10-20
lines each) that still exhibits the problem. You just might discover
the problem while you're doing this; if not, post again.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
You put the functions in program.h? That's why. Header files are
Actually, I didn't. That kind of confuses me too. I just have the
prototypes, 3 structs, and some extern global variables in the header file.
I define these in my program.c file.
#ifndef only works within the same compilation unit. For example,
it WILL protect you from including "program.h" twice, or from
including "program.h" and "foo.h" which includes "program.h" . It
will NOT protect you from "program.h" already being included in
another compilation unit. The compiler is not psychic. It does


I see what you mean. The compiler compiles them each individually
and can't see in to the future to see the .o files will be linked.

I will try a different approach.

Thanks so much!

John

Nov 14 '05 #5
John Hanley wrote:
I have a program where both my main.c and program.c files use the program.h
file. So I #include "program.h" in both the .c files. The program.h file
has
#ifndef PROGRAM_H
#define PROGRAM_H
...
#endif

Yet when I build my program, the DJGPP compiler tells me there are multiple
definitions of each of my functions.

Any ideas as to what I doing wrong here? cat program.h #ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(voi d); // declaration
#endif//GUARD_PROGRAM_H
cat program.c #include "program.h"

void my_function(voi d) {// definition
fprintf(stdout, "Hello from my_function(voi d)!\n");
}
cat main.c #include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
gcc -Wall -std=c99 -pedantic -o main main.c program.c

Nov 14 '05 #6
> > cat program.h
#ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(voi d); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(voi d) {// definition
fprintf(stdout, "Hello from my_function(voi d)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c


This exactly what I did. But in Rhide, it tells me "multple definitions of
_function", and at the command line it tells me "undefined reference to
_function". Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?

Thanks!

John
Nov 14 '05 #7
"John Hanley" <jd******@telus planet.net> writes:
> cat program.h

#ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(voi d); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(voi d) {// definition
fprintf(stdout, "Hello from my_function(voi d)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c


This exactly what I did. But in Rhide, it tells me "multple definitions of
_function", and at the command line it tells me "undefined reference to
_function". Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?


It's *exactly* what you did?

Try compiling *exactly* the code above and see whether you get the
same error.

If not, as I suggested elsethread, trim your own code down to a short
example that exhibits the problem.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8
John Hanley wrote:
> cat program.h #ifndef GUARD_PROGRAM_H
#define GUARD_PROGRAM_H 1
#include <stdio.h>
void my_function(voi d); // declaration
#endif//GUARD_PROGRAM_H
> cat program.c

#include "program.h"

void my_function(voi d) {// definition
fprintf(stdout, "Hello from my_function(voi d)!\n");
}
> cat main.c

#include <stdlib.h>
#include "program.h"

int main(int argc, char* argv[]) {
my_function();
return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c program.c

This exactly what I did.


No it isn't exactly what you did.
But in Rhide, it tells me "multple definitions of _function",
and at the command line it tells me "undefined reference to _function".
Yet what you have above is just how I set it up.
I am unsure why it is doing that.
Any other ideas?


Yes.

Stop being such a twit!

We can't read your mind.
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."

Otherwise, stop bothering us.
Nov 14 '05 #9
> No it isn't exactly what you did.

Then perhaps you can point out what I did wrong. What I did was declare my
functions, global variables and structs in the .h file. I then #included
them in my .c files as you have done in the example program.
Yes.

Stop being such a twit!

We can't read your mind.
I thought perhaps there was some broader problem I was overlooking. I am
not an experienced
programmer that is why I am asking questions on the newsgroup.
Do as Keith Thompson asked:

"Try narrowing your program down to a set of very small files
(say 10-20 lines each) that still exhibits the problem."
That's fine. I was only replying to your email as you emailed me.

Otherwise, stop bothering us.


No one is forcing you to read my posts and nobody has a gun to your head
telling you
to answer them. If you don't like my questions, you leave ME alone. I
would really
not be a bit offended if you didn't answer my posts.

I was in no way being rude in asking any questions and I appreciated all the
responses I've received as I realize people take time to try and help. You,
however, were rude. You could've been polite about it, but you weren't. I
guess that's your choice...


Nov 14 '05 #10

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

Similar topics

5
1867
by: John Gabriele | last post by:
I'm hoping someone can please help me remember the C++ rule: When you're writing a header file for a class (say, some_namespace::Bar), and that class makes use of another class (some_namespace::Foo), -------------------------------- snip -------------------------------- #ifndef GUARD_Foo_HPP #define GUARD_Foo_HPP namespace some_namespace...
3
3201
by: Michael Sgier | last post by:
Hello I get the error below. But why and what does ifndef? Well what is ifndef? Many thanks and regards Michael error: syntax error before `(' token #ifndef MD2SwapInt static __inline__ Uint32 MD2SwapInt(Uint32 D) {
1
2746
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value...
9
28673
by: Qiao Jian | last post by:
I am new to c. Today I just read an h file within which there is statements: #ifndef _RANDOM_H #define _RANDOM_H So what is the meaning or purpose of this statement? When should I use such statement in my own program? Thank you so much!
2
1508
by: praveenkojha | last post by:
Hi, I am novice in C++ and am more of a C# guy. I have a third party C++ code which I want to create and use as a managed assembly. I have created a .NET win32 application and have copied this C++ code here and made changes according to my needs. However I am always getting this linker error when compiling. I looked a lot n the web and...
4
1118
by: Jamiil | 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 include in its header file. However, there are times when some of the other classes are not and will not be dealt with, thus the need to include the...
25
2118
by: David Sanders | last post by:
Hi, As part of a simulation program, I have several different model classes, ModelAA, ModelBB, etc., which are all derived from the class BasicModel by inheritance. model to use, for example if the parameter model_name is "aa", then choose ModelAA. Currently I do this as follows:
14
2175
by: aaragon | last post by:
Hi everyone, I've been writing some code and so far I have all the code written in the .h files in order to avoid the linker errors. I'm using templates. I wanted to move the implementations to the .cpp files. After some reading, I found that the only way to do this is to add the actual template instantiations in the .cpp file. But, how do...
2
2140
by: sydneytroz | last post by:
I'm getting a rather odd set of error messages when I try compiling the below code. I'm using the Dev-C++ IDE on WinXP, and am using a small driver program (unit-dev.cpp) to test the classes (it includes both the file listed below) The messages are: In file included from unit-dev.cpp:27: D:/Documents and Settings/Zachary/My...
0
7694
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7609
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7921
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.