0

I had an error of already declared because I declared the same variable at two files, So I did like this:

open_gl.h

#ifndef _OPEN_GL_H_
#define _OPEN_GL_H_
#define SCREEN_SIZE 500
#define SCREEN_POINT 0.7
int position;
extern int openGl(int *argc, char *argv[]);
extern void navigate(const char *routeName);
#endif // !OPEN_GL

and I'm trying to use the position variable in other files. but I get this error:

Severity Code Description Project File Line Suppression State Error LNK2005 "int position" (?position@@3HA) already defined in login.obj sudoku C:\Users\nati3\source\repos\sudoku\sudoku\registerPage.obj 1

when I remove the declaration from the header file I get:

Severity Code Description Project File Line Suppression State Error C2065 'position': undeclared identifier sudoku c:\users\nati3\source\repos\sudoku\sudoku\login.cpp 8

and other errors about other files that it wasn't declared.

How can I fix it?

Kaki Baleven
  • 355
  • 3
  • 7
  • 19

1 Answers1

0

Declare in one compilation unit only.

In all other declare it as extern

example:

x.c

int x;

y.c

extern int x;
0___________
  • 60,014
  • 4
  • 34
  • 74
  • I tried it, but I got this error: `Severity Code Description Project File Line Suppression State Error LNK2001 unresolved external symbol "int position" (?position@@3HA) sudoku C:\Users\nati3\source\repos\sudoku\sudoku\login.obj 1 ` – Kaki Baleven Jul 28 '18 at 10:47
  • Hi, 10x, I declared it also in the source file and it works – Kaki Baleven Jul 28 '18 at 10:50