Вопрос

i have a problem in C which i don't know how to solve. Suppuse i have 4 c files, a.c ; b.c ; c.c ; d.c, and for each one of them there is an .h file: a.h ; b.h ; c.h ; d.h, which of course they include. I want to do the following:

a.h will contain:

#include "b.h"
#include "c.h"
...

b.h will contain:

#include "d.h"
...

and c.h will also contain:

#include "d.h"
...

In d.h I define a struct, for example, the content of d.h will be:

typedef struct address { 
   int address; 
} address;

The problem is that i get the error (when i compile the files):

In file included from c.h:1:0,
                 from a.h:2,
                 from a.c:1:
d.h:3:16: error: redefinition of ‘struct address’
d.h:3:16: note: originally defined here
d.h:5:3: error: conflicting types for ‘address’
d.h:5:3: note: previous declaration of ‘address’ was here

I understand why is it happening (because the preproccesor import the definition twice), but how do i solve it? I need the includes to be that's way (because of course i have more files, this was just an example). What can i do?

note: my makefile was:

project: a.o b.o c.o d.o
    gcc -g a.o b.o c.o d.o -ansi -Wall -pedantic -o output

a.o: a.c a.h  b.c b.h c.c c.h d.c d.h 
    gcc -c a.c -ansi -Wall -pedantic -o a.o

b.o: b.c b.h d.c d.h
    gcc -c b.c -ansi -Wall -pedantic -o b.o

c.o: c.c c.h d.c d.h
    gcc -c c.c -ansi -Wall -pedantic -o c.o

d.o: d.c d.h
    gcc -c d.c -ansi -Wall -pedantic -o d.o

and i think it's okay.. It workd for me. Thank you for the help.

Это было полезно?

Решение

Enclose the content of each header file in preprocessor guards:

/* a.h */
#ifndef A_H_
#define A_H_

/* content of a.h goes here */

#endif

Same for the other headers:

/* b.h */
#ifndef B_H_
#define B_H_

/* content of b.h goes here */

#endif

and so on.

That's the usual way to ensure that each header content is read only once. (When the file is read for the first time, the symbol [e.g. B_H_] has not yet been defined, so ifndef is true, and the content is read. From the second time on, the symbol is defined, and the content is skipped.)

Note that the name of the symbol A_H_ is arbitrary, but usually people use the name of the header written in capital letters. Hence the mechanism fails as soon as you include two different headers with the same name, unless you use different names for the guards.

You may also want to google for #pragma once, which many preprocessors support, but which is not standard.

Другие советы

You can use "includeguards" to prevent the preprocessor from putting the contents of header-files multiple times into your preprocessed source-code.

Using

#ifndef A_H_
#define A_H_

you define a symbol called A_H_. If the preprocessor hits this symbol another time, it will ignore what is between

#ifndef A_H_
#define A_H_

and

#endif

There are also specific compiler-commands like #pragma once, this works with Visual Studio for example and does the same as the include-guards do. Check if your compiler provides such things.

Add Once-Only Headers. Refer the below link for more information.

http://gcc.gnu.org/onlinedocs/cpp/Once_002dOnly-Headers.html#Once_002dOnly-Headers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top