Jonson Bylvaklov
Jonson Bylvaklov

Reputation: 905

C struct getting doubly included

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?

Upvotes: 0

Views: 82

Answers (2)

Bill Lynch
Bill Lynch

Reputation: 81916

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:

Upvotes: 2

rid
rid

Reputation: 63442

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.

Upvotes: 4

Related Questions