user647868
user647868

Reputation: 13

redefinition of struct in linux GCC

I am a beginner in programming.I try to compile a c program in linux, gcc written by the others and got the following error .

cmd.h:145: error: redefinition of ‘struct stat’.

It seems somebody has defined the struct 'stat' more than once. But since there are lots of code files, i dont know how to solve it.Can anyone advise me on that. Thanks

Upvotes: 1

Views: 8301

Answers (4)

Buhake Sindi
Buhake Sindi

Reputation: 89169

I would suggest to create a makefile and use make to compile your code. Example.

Upvotes: 1

Jim Balter
Jim Balter

Reputation: 16406

Compile with cc -E, which will produce the preprocessor output. Scan that for occurrences of struct stat; the # filename lineno lines emitted by the preprocessor will tell you where the definition occurs.

=== edit ===

Even better: Compiling

#include <sys/stat.h>

struct stat {};

produces the messages

foo.c:3:8: error: redefinition of ‘struct stat’
/usr/include/bits/stat.h:43:8: note: originally defined here

Which says exactly where the clash occurs. I'll bet that you get similar messages and have simply overlooked it.

Upvotes: 0

pmod
pmod

Reputation: 10997

I suppose you try to define own structure type, which is already defined in standard headers. struct stat is defined in sys/stat.h see here (containing file stat info) and it's included directly or through other headers.

A better approach is to use prefix for your type definition, for example, typedef struct myprog_cmd_stat { ... }; . The latter will also allow to quickly understand where it's defined.

Upvotes: 2

Steve-o
Steve-o

Reputation: 12866

Try using grep -r "struct stat" * to find the definition? Try your own code first then check for definitions in /usr/include and /usr/local/include.

Upvotes: 0

Related Questions