0

I want have two shared structs.

In my .h:

typedef struct {
  char nombre[25];
  int pv;
  char ataque1[25];
  char ataque2[25];
  char ataque3[25];
  char ataque4[25];
  char ataque1_estado[25];
  char ataque2_estado[25];
  char ataque3_estado[25];
  char ataque4_estado[25];
  char estado[25];
  int pd_max[4];
  int pd_min[4];
  int pp[4];
  int pociones;
  int antidotos;
} pokemon;

extern pokemon pokemon1;
extern pokemon pokemon2;

And in my .c files:

#include "file.h"
//file body, i use here structs in file.h

But it doesn't compile. GCC shows this error:

main.c:(.text+0x440): referencia a `pokemon1' sin definir

What is wrong?

3
  • @atturri Yes, but i want use the same structures in both .c files, sharing the structures info. Commented Apr 20, 2016 at 16:55
  • Yes, you will, don't worry. You must define the objects in a unique .c file, but thanks to the extern in the .h file the other .c files will be using the same objects. Commented Apr 20, 2016 at 17:08
  • @atturri Thanks a lot! i understand and compile very well :) Commented Apr 20, 2016 at 17:18

1 Answer 1

3

The last two lines in your header file are the declaration of your pokemon objects, which allow you to use them from multiple .c files. But in one of the .c files you must define them, which in your case is the same as declaring but without the extern keyword:

pokemon pokemon1;
pokemon pokemon2;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.