473,738 Members | 11,192 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
25 4907
On Thu, 26 Aug 2004 17:20:11 -0600, "John Hanley"
<jd******@telus planet.net> wrote:
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...


I had to look up the message ID to see who you were responding to,
since you didn't provide attributions. As it turns out, I couldn't
tell by the threading because I have ERT permanently killfiled. Ignore
him. Most everyone else does.

--
Al Balmer
Balmer Consulting
re************* ***********@att .net
Nov 14 '05 #11
"E. Robert Tisdale" wrote:
John Hanley wrote:
.... snipped ...
Stop being such a twit!
.... snip ...
Otherwise, stop bothering us.


Ignore Trollsdale. Its primary purpose is to annoy and give
misinformation. It is a well known troll around here.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #12
"John Hanley" <jd******@telus planet.net> writes:
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.


We can't point out what you did wrong without knowing exactly what you
did. Sometimes we can figure out the problem from a brief
description; in this case, we can't.

I suspect the problem is in some detail that you're not telling us,
perhaps because you've assumed it's not significant. That's
understandable; if you were knowledgeable enough to know exactly
what's significant and what isn't, you probably wouldn't have to come
to us for help. Since you are coming to us for help, you really
should give us the information we're asking for. We need a verbatim
cut-and-paste *brief* example of the C code that's causing the
problem. Without that, we can't help you.

[...]
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.

[...]

This is Usenet, not email. The difference is significant; though your
remarks here might be aimed at one person, there are many more
listening in.

As for the problem you're actually asking about, we've now explained
several times what you need to tell us before we can help you. Your
description, quoted above, is not enough. ERT was unnecesarily rude
in my opinion, but he's absolutely right about at least one thing: we
can't read your mind.

You can continue describing what you're doing, and we can continue
telling you we don't know what the problem is. Or you can show us
what you're doing, and we can probably help. The choice is yours (but
one of the choices would be a collosal waste of time and, well, rude.)

--
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 #13
John Hanley wrote:
You, however, were rude.
You could've been polite about it, but you weren't.


I didn't mean to be rude.
I was trying to get your attention.
We can't help you unless you do as Keith suggested.
Nov 14 '05 #14
Alan Balmer wrote:
John Hanley wrote:

I had to look up the message ID to see who you were responding to,
since you didn't provide attributions. As it turns out,
I couldn't tell by the threading
because I have ERT permanently killfiled.
And I'm grateful for that.
Ignore him. Most everyone else does.


Do you intend to help John Hanley with the problem that he describes?
Nov 14 '05 #15
>> > 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",


There are no references to _function in any of the code above.
What, EXACTLY, did the message say?
and at the command line it tells me "undefined reference to
_function".
Again, there are no references to _function in any of the code above.
WHERE did it say this reference was?

Yet what you have above is just how I set it up. I am unsure
why it is doing that. Any other ideas?


Gordon L. Burditt
Nov 14 '05 #16
In article <10************ **@proxy2.srv.u alberta.ca>,
John Hanley <jd******@telus planet.net> wrote:
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".


Are you *absolutely sure* that you only have prototypes in the .h file,
and not definitions? Please post the declaration of "function" from your
..h file.

-- Richard
Nov 14 '05 #17

"Keith Thompson" <ks***@mib.or g> wrote in message
news:ln******** ****@nuthaus.mi b.org...
"John Hanley" <jd******@telus planet.net> writes:
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.


We can't point out what you did wrong without knowing exactly what you
did. Sometimes we can figure out the problem from a brief
description; in this case, we can't.

I suspect the problem is in some detail that you're not telling us,
perhaps because you've assumed it's not significant. That's
understandable; if you were knowledgeable enough to know exactly
what's significant and what isn't, you probably wouldn't have to come
to us for help. Since you are coming to us for help, you really
should give us the information we're asking for. We need a verbatim
cut-and-paste *brief* example of the C code that's causing the
problem. Without that, we can't help you.


Sounds good. I'll post that at the bottom of this reply.

[...]
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.

[...]

This is Usenet, not email. The difference is significant; though your
remarks here might be aimed at one person, there are many more
listening in.


True. I actually was replying to an email. Mr. Tisdale had replied to my
post
over email and cc'd the newsgroup. I did a reply all and replied to both.
I
should've realized it probably shouldn't have gone to the newsgroup as well
(being more
of a personal reply). My apologies for breaking any Usenet etiquette.

You can continue describing what you're doing, and we can continue
telling you we don't know what the problem is. Or you can show us
what you're doing, and we can probably help. The choice is yours (but
one of the choices would be a collosal waste of time and, well, rude.)
Ok, here's my header file r_climat.h

struct data_record
{
...
};

struct data_list
{
struct data_record * head;
struct data_record * tail;
};

struct parameter_list
{
...
}parameters;

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

/* Global variables */
extern int FORMAT_TYPE;
extern char PARAM_FILE[256];
extern char * IN_FILE;
extern char OUT_FILE[13];

extern void init(struct data_list * l);
extern struct data_record * new_node(long int sid, long int date, int year,
int month, int element, long int val[96], char f[32]);
extern void add_to_tail(str uct data_list * l, long int sid, long int date,
int year, int month, int element, long int val[96], char f[32]);
extern int remove_record(s truct data_list * ls, struct data_record * rec);
extern void get_filename(ch ar * filename, char * outname);
extern int validate_format (char * f);
extern void print_list(stru ct data_list * dl);
extern int format_HLY(char * f);
extern int f_eat_spaces(FI LE * f);
extern int get_parameters( );
extern void empty_list(stru ct data_list * d);
extern int get_number_of_d ays(struct data_record * d);
extern int to_CSV_DLY(char * filename, struct data_list * list);
extern int format_DLY(char * f, struct data_list * dl);
extern int format_MLY(char * f);
extern int format_FIF(char * f);

I tried with and without the #ifndef/#define/#endif and as well with and
without the extern on the functions.

I was able to figure out what was wrong with the undefined references to my
global variables. I declared them here, but not in my .c file. So I don't
have that error anymore.

As for my "multiple declaration" problem, here's the snippet of my .c files:
main.c
#include "r_climat.h "

#define HLY 0
#define DLY 1
#define MLY 2
#define FIF 3

int FORMAT_TYPE;
char PARAM_FILE[256];
char * IN_FILE;
char OUT_FILE[13];

int main(int argv, char * argc[])
{
main program here
}

and here's r_climat.c


#include "r_climat.h "

#define HLY 0
#define DLY 1
#define MLY 2
#define FIF 3

/* initialize head and tail of data_list to NULL */
void init(struct data_list * l)
{
l->head=NULL;
l->tail=NULL;
}

and the rest of my functions that would take another 800 lines or so.

I only define them in my r_climat.c (I know I don't define any in main.c)
and I only prototype them
in r_climat.h. They all compile just fine with no errors or warnings. It
is during linking that I get this
"multiple definitions" error.

Let me know if there is anything else specific you would like.

Thanks again for all your help. I really do appreciate all of it.

Best regards,
John


Nov 14 '05 #18
to get an accurate answer, please post your entire source code.

chok
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?

Thanks!

John


Nov 14 '05 #19
Ok, it seems my code wasn't what was buggy. I am using the RHIDE ide for
djgpp. When I built it at the command line just now it didn't give me
errors. I think what I did was when I created my "project" in RHIDE, I
added my files including my .h file. I just removed my .h file from the
project and it didn't give me any errors in RHIDE.

Sorry for taking up all this time on such a small oversight. I really do
appreciate the replies and all the help.

Thanks so much!

John


Nov 14 '05 #20

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

Similar topics

5
1880
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
3207
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
2760
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 all across our projects.
9
28683
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
1513
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 followed all instructions so far of adding msvcrt.lib, glu32.lib, opengl32.lib to the project...
4
1123
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 header files does not arrive. To handle this, I have used compiler preprocessors to prevent the...
25
2144
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
2200
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 you do that if you're not working with built-in types? For example, a template class might be,
2
2153
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 Documents/School/G11/Computer Science/VirtOS/src/ipc/messagehandler.h:27: error: using-declaration for non-member at...
0
8788
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.