0

I have a header file which has an include guard setup. I have multiple C files in my project that require this header file for compilation. When I go to compile however I get an error saying that the function has already been included from another file. Shouldn't the include guard prevent this from happening? In theory I believe I should be able to import this file a bunch of times and not have this issue.

#ifndef __BST_INCLUDED
#define __BST_INCLUDED__

//bunch of code here

#endif

Error:

bst.h:22:13: error: conflicting types for ‘pruneBSTNode’
 extern void pruneBSTNode(bst *tree,bstNode *node);
             ^
In file included from vbst.h:5:0,
                 from bstrees.c:7:
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
PleaseNoBugs
  • 169
  • 1
  • 10
  • Perhaps use `#pragma once` – Ed Heal Mar 10 '17 at 02:02
  • 2
    Regarding __BST_INCLUDED, fear the double underscore! [Double underscore is reserved for use by the library implementation](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) Bad and really freaky, hard to debug things can happen if you do this and collide with some library identifier, so just don't. – user4581301 Mar 10 '17 at 02:17
  • @user4581301: So is a single underscore at file-scope. – too honest for this site Mar 10 '17 at 02:29

3 Answers3

5
#ifndef __BST_INCLUDED
#define __BST_INCLUDED__
//bunch of code here
#endif

This will not protect anything. For the simple reason that __BST_INCLUDED__ is not the same as __BST_INCLUDED, and __BST_INCLUDED never gets defined.

But also:

bst.h:22:13: error: conflicting types for ‘pruneBSTNode’
 extern void pruneBSTNode(bst *tree,bstNode *node);
         ^
In file included from vbst.h:5:0,
                 from bstrees.c:7:

this error is not telling you that "the function has been included from another file", it's a completely unrelated error. The "included from" part is just telling you how the compiler got to the line displayed after it (which is missing from the question).

user253751
  • 57,427
  • 7
  • 48
  • 90
0

Your include guards are fine. The problem is that you've declared multiple different signatures for the pruneBSTNode function. Make sure the header and the .c file agree on the return type and argument types.

Dominic Dos Santos
  • 2,623
  • 2
  • 18
  • 30
0
__BST_INCLUDED

is not the same as

__BST_INCLUDED__. 

Besides, when it comes to compiling headers, my recommendation is that you use more common convention for your include guards

#ifndef FILE_NAME_HPP
#define FILE_NAME_HPP


#endif

But alas, like the others said. Your error isn't coming from there.

Nova Ardent
  • 161
  • 1
  • 16