-1

AS the title says I am having an issue using include guards. I was not sure if I was using them correctly and so I have already checked several related posts and they all contain code that seems to be the same as mine. Please advise me as to what is wrong with my use of include guards.

For context I have several header files that I want to be able to use in other programs, because of this multiple files include the same dependency header (a linkedList file) and this is where the issue arises. Even though I appear to have include guards the compiler still reports that the code has a redefinition error. Below is the include guard that isn't working.

 #ifndef UNI_LINKED_LIST_LIB_H
 #define UNI_LINKED_LIST_LIB_H "uniLinkedListLibV02.h"
 #include UNI_LINKED_LIST_LIB_H
 #endif

My understanding is that the #ifndef will return false if I try to include this header more than once. And on the first include it should define UNI_LINKED_LIST_H and then include the library.

  • 1
    If the guard is for the `.h` file you're trying to include, do _not_ try to define the guard variable. Replace all that you show with a simple: `#include "uniLinkedListLibV02.h"`. It is the responsibility of the included file to handle _its_ guard variable – Craig Estey Apr 22 '20 at 00:03
  • This is something I did not know. Thank you. – cooperg2001 Apr 22 '20 at 00:33

2 Answers2

4

Here's a .h file with a guard:

// guard.h -- include file with a guard

#ifndef GUARD_H
#define GUARD_H

#define SPECIAL_VALUE   23

#endif

Here's a .c file that uses it:

// main.c -- program that includes the guarded file

#include <stdio.h>

#include "guard.h"

int
main(void)
{

    printf("%d\n",SPECIAL_VALUE);

    return 0;
}

Here is a .c file that includes the file incorrectly:

// bad.c -- program that includes the guarded file _incorrectly_

#include <stdio.h>

// do _not_ do this:
#ifndef GUARD_H
#define GUARD_H
#include "guard.h"
#endif

int
main(void)
{

    printf("%d\n",SPECIAL_VALUE);

    return 0;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
  • Thank you for your answer. I just swapped the post answer to this one since it provides info on what not to do as well. – cooperg2001 Apr 22 '20 at 00:18
  • The key bad point is the `#define GUARD_H` in the including code. The header must still include a header guard. If you define `GUARD_H` before the header is included, the compiler won't see the header's contents. Omitting the `#define GUARD_H` from the calling code brings you to a grey area. If the header guard name is readily deducible from the header name, you can optimize the build a bit by using the `#ifndef` / `#endif` around the headers, but it quickly becomes too cumbersome to be worthwhile. You need to wrap each `#include` with two extra lines… It may be OK, but it is probably not. – Jonathan Leffler Apr 22 '20 at 03:46
-2

This is all you need:

 #ifndef UNI_LINKED_LIST_LIB_H
 #define UNI_LINKED_LIST_LIB_H 
 #include "uniLinkedListLibV02.h"
 rest of code goes here
 #endif

You just need to define UNI_LINKED_LIST_LIB_H, the value doesn't matter.

jmq
  • 1,559
  • 9
  • 21
  • Thank you. Now everything looks way cleaner. In addition, I found that some code was in the wrong order which was causing issues as well. – cooperg2001 Apr 22 '20 at 00:16
  • 2
    This is not how include guards should be implemented. The guards should be in the file being included, so that they will operate regardless of whether the including file implements this or not. – Eric Postpischil Apr 22 '20 at 00:41