0

I can't figure this out...

I have object.h which looks like so

struct basicObject {
       int x, y;
}

void objectSet (int x, int y);

Now I need to include object.h in my main file but I also need the objectSet function and struct in a different file called svg.c

svg.h looks like

#define OUTPUT_FILE "output.svg"
#include "object.h"

void saveSVG (basicObject item);

But my main file also includes svg.h! So I'm getting 'redifinition errors' of struct basicObject. This clearly has something to do object.h getting included twice. How can I fix this?

1
  • If you're not compiling as C++ or have edited out a typedef, in saveSVG, you'd need struct basicObject. Commented Oct 4, 2012 at 22:40

2 Answers 2

4

You should use include guards if you plan on using #include to refer to the same header file more than once, but you only need to include it the first time.

Sign up to request clarification or add additional context in comments.

Comments

2

There are two main options. In your header file, do

#pragma once

or wrap the entire header file in:

#ifndef MY_SVG_H
#define MY_SVG_H

... your code ...

#endif

Further reading:

2 Comments

(do note however that #pragma once is non-standard)
Suggest you to make both, #pragma once #ifndef .. #define .. => You keep advantage of fast compilation with pragma once, but portability with define.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.