0

So I have a header file let's say "header.h" which is protected as follows:

#ifndef __HEADER1_H
#define __HEADER1_H
//type and function def 
typedef struct
{
  float r; //process noise
  float k; //process gain
}state_t;

int get_state(state_t* state, float b);

#endif

Now I have two other headers which I defined as follows:

 #ifdef __HEADER2_H
 #include "header.h"
//function def
 #endif

Second header:

#ifdef __HEADER3_H
//function
//the reason it is done this way is for cnditional compiling such that if the caller  

//defines __HEADER3_H t this file won't be included.
#include "header.h"
#endif

Now as I suspected the compiler complained that types and functions defined in header.h were not detected in the source implementation of header2 and header3. So I included header.h in the source files as well. Now the linker is complaining functions that are defined in header.h are multiply defined. My understanding was since the header.h is protected by ifndef it will only be included once so I don't see the problem. here is the error that I am getting:

Symbol get_state multiply defined(by kalman.o and dsp.o)

Is there any chance that I am doing something unusally wrong?

5
  • Yep, see the comments below -- you've got several things wrong. Commented Oct 3, 2012 at 2:31
  • What are you trying to do with the second and third code segments?? Commented Oct 3, 2012 at 2:34
  • I'm thinking at this point that it might make sense to paste in the actual header.h file (or at least enough to show the top, bottom, and at least one item the compiler complains about) -- to avoid further typos from making it impossible to answer in a helpful manner. Commented Oct 3, 2012 at 2:36
  • Essentially I am trying to avoid compiling if there __HEADER2_H and __HEADER3_H are not defined. Commented Oct 3, 2012 at 2:45
  • 1
    The "multiply defined" error does not mean you are delcaring the function twice (which is what multiple inclusions of header1.h, as you've pasted it here, would do). It means you have implemented the function twice - which could mean you implemented it in the header file, causing both kalman.c and dsp.c to implement it, or could mean both of those source files actually contain the implementation (which is one too many). Commented Oct 3, 2012 at 2:52

2 Answers 2

2
#ifndef __HEADER1_H
#define __HEADER_H

The problem is your guard (__HEADER_H) is different from what you are checking for (__HEADER1_H). Make these both the same value.

Sign up to request clarification or add additional context in comments.

2 Comments

And you don't show the necessary #define in your second and third headers. Your second and third headers use #ifdef rather than #ifndef,, and don't have the required #define. Also, identifiers starting with _ are reserved to the implementation; these aren't likely to cause problems, but you should use identifiers like HEADER1_H.
I made a typing mistake here. In my code I used __HEADER1.h. But good point Keith. I modiied that in my code although the error still shows up.
1

The typical "guard" for a header file is:

myheader.h:

#ifndef _MYHEADER
#define _MYHEADER
<do stuff>
#endif

Optionally where myheader.h is included, you can do:

#ifndef _MYHEADER
#include "myheader.h"
#endif

This is optional and basically is only to improve compile performance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.