0

When i declare a global variable, I get the error:

linker command failed with exit code 1 (use -v to see invocation)

Here is the code:

import "menuplay.h"

import "buttonmanager.h"

int  test; //<--------------when i  declare  it show  error Apple Mach-O Linker Error

@interface lessonone : CCLayer {
...
}
kenorb
  • 155,785
  • 88
  • 678
  • 743
Tanakorn.K
  • 150
  • 9

1 Answers1

1

Declare it static:

static int test;

Or const if its value should never change:

const int test = 10;
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • static int test; Can change value in other method ? This it no error but i define value in menu.mm test=10 when i show test in menuplay.mm value =0,why not =10? – Tanakorn.K Mar 16 '13 at 16:17
  • because it's a different variable. In menuplay.mm you will have to declare it as: extern int test; – CodeSmile Mar 16 '13 at 17:17
  • http://stackoverflow.com/questions/8808159/objective-c-global-variables All you need is to use plain old C global variables. First, define a variable in your main.m, before your main function: #import <...> // Your global variable definition. type variable; int main() { ... Second, you need to let other source files know about it. You need to declare it in some .h file and import that file in all .m files you need your variable in: // .h file // Declaration of your variable. extern type variable; – Tanakorn.K Mar 16 '13 at 18:39