How can a declaration conflict with itself?

eeeeaaii picture eeeeaaii · Aug 24, 2010 · Viewed 24.2k times · Source

This is the error I'm getting when trying to compile some code that uses taucs (not my code):

.../taucs/src/taucs.h:554: error: conflicting declaration ‘typedef struct taucs_ccs_matrix taucs_ccs_matrix’
.../taucs/src/taucs.h:554: error: ‘taucs_ccs_matrix’ has a previous declaration as ‘typedef struct taucs_ccs_matrix taucs_ccs_matrix’

wat? It is conflicting with itself?

After I pinched myself, I created a test header and put in a conflicting definition, just to make sure I was right about this:

In file testit.h:

#include "somethingelse.h"

typedef struct
{
  int n;
} foobar;

In file somethingelse.h:

typedef struct
{
  int n;
} foobar;

Sure enough, I get:

testit.h:6: error: conflicting declaration ‘typedef struct foobar foobar’
somethingelse.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’

Or if I have this in testit.h:

typedef struct
{
  int n;
} foobar;

typedef struct
{
  int n;
} foobar;

testit.h:9: error: conflicting declaration ‘typedef struct foobar foobar’
testit.h:4: error: ‘foobar’ has a previous declaration as ‘typedef struct foobar foobar’

The line number is always different -- a declaration can't conflict with itself. I don't get it. Anyone ever seen this?

Answer

Harper Shelby picture Harper Shelby · Aug 24, 2010

Is the single header included in multiple source files? If so, you need to wrap it in "include guards" like so:

#ifndef TAUCS_H
#define TAUCS_H

//Header stuff here

#endif //TAUCS_H