Header/Include guards don't work?

2019-02-17 14:34发布

For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below:

main.c:

#include "thing.h"

int main(){
    printf("%d", increment());

    return 0;
}

thing.c:

#include "thing.h"

int increment(){
    return something++;
}

thing.h:

#ifndef THING_H_
#define THING_H_

#include <stdio.h>

int something = 0;

int increment();

#endif

When I attempt to compile this, GCC says that I have multiple definitions of the something variable. ifndef should make sure that this doesn't happen, so I'm confused why it is.

6条回答
一夜七次
2楼-- · 2019-02-17 15:10

You have one definition in each translation unit (one in main.c, and one in thing.c). The header guards stop the header from being included more than once in a single translation unit.

You need to declare something in the header file, and only define it in thing.c, just like the function:

thing.c:

#include "thing.h"

int something = 0;

int increment(void)
{
    return something++;
}

thing.h:

#ifndef THING_H_
#define THING_H_

#include <stdio.h>

extern int something;

int increment(void);

#endif
查看更多
Lonely孤独者°
3楼-- · 2019-02-17 15:11

what ifndef is guarding is one .h included in a .c more than once. For instance

thing. h

#ifndef
#define

int something = 0;
#endif

thing2.h

#include "thing.h"

main.c

#include "thing.h"
#include "thing2.h"
int main()
{
  printf("%d", something);
  return 0;
}

if I leave ifndef out then GCC will complain

 In file included from thing2.h:1:0,
             from main.c:2:
thing.h:3:5: error: redefinition of ‘something’
thing.h:3:5: note: previous definition of ‘something’ was here
查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-17 15:14

The variable something should be defined in a .c file, not in a header file.

Only structures, macros and type declarations for variables and function prototypes should be in header files. In your example, you can declare the type of something as extern int something in the header file. But the definition of the variable itself should be in a .c file.

With what you have done, the variable something will be defined in each .c file that includes thing.h and you get a "something defined multiple times" error message when GCC tries to link everything together.

查看更多
放我归山
5楼-- · 2019-02-17 15:19

The include guards are functioning correctly and are not the source of the problem.

What happens is that every compilation unit that includes thing.h gets its own int something = 0, so the linker complains about multiple definitions.

Here is how you fix this:

thing.c:

#include "thing.h"

int something = 0;

int increment(){
    return something++;
}

thing.h:

#ifndef THING_H_
#define THING_H_

#include <stdio.h>

extern int something;

int increment();

#endif

This way, only thing.c will have an instance of something, and main.c will refer to it.

查看更多
爷的心禁止访问
6楼-- · 2019-02-17 15:22

try to avoid defining variables globally. use functions like increment() to modify and read its value instead. that way you can keep the variable static in the thing.c file, and you know for sure that only functions from that file will modify the value.

查看更多
劫难
7楼-- · 2019-02-17 15:28

The header guards will stop the file from being compiled more than once in the same compilation unit (file). You are including it in main.c and thing.c, so it will be compiled once in each, leading to the variable something being declared once in each unit, or twice in total.

查看更多
登录 后发表回答