Hello there, I have here a problem with the compilation of my program. The compiler tells me that my typedefs are redefined, but I defined it only once. Ok, heres the score, I have here a header file (let's name this as file X) that contains all the typedefs that I need (so that I will be able to reuse it to other files). I have included it to the two files (let's say, file A and file B) that really needs it badly and these two files (file A and B) are included to a file that needs them too (let's call this one as file C). Whenever I compile file C, I get the error of redefinition. Is there something wrong with the way I mess with the files? I'm using gcc thus involving Makefile.

Thanks! :)

you header file need code gards to prevent that, like this

#ifndef MYHEADER
#define MYHEADER
// code goes here

#endif
Member Avatar for GreenDay2001

You could also use #pragma once but it is compiler dependent and deprecated. Its generally used with MSVC++.

commented: Hell yeah +2

>You could also use #pragma once but it is compiler dependent and deprecated.
All pragmas (well, most with the coming of C99) are compiler dependent. You should avoid them whenever possible. And if #pragma once is compiler dependent, how can you say that it's deprecated? Have all compilers documented it as deprecated? Anyway, the only real advantage of #pragma once is that you don't have to worry about naming collisions in your inclusion guards, and that's not worth the loss of portability.

commented: :) +2
Member Avatar for GreenDay2001

>You could also use #pragma once but it is compiler dependent and deprecated.
All pragmas (well, most with the coming of C99) are compiler dependent. You should avoid them whenever possible. And if #pragma once is compiler dependent, how can you say that it's deprecated? Have all compilers documented it as deprecated? Anyway, the only real advantage of #pragma once is that you don't have to worry about naming collisions in your inclusion guards, and that's not worth the loss of portability.

Of course all pragmas are compiler dependent and different compilers could have different pragmas. I didn't mentioned it all but I think its pretty clear. By deprecated I meant its somewhat strongly not advised. I have written what I have read and inferred from it sometime. somewhere.

I placed #pragma once in file X and it worked. Thanks guys. :)

Member Avatar for GreenDay2001

Didn't this worked as suggested by Ancient Dragon . Its better to use that and you must know how to use it

#ifndef MYHEADER
#define MYHEADER
// code goes here

#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.