Skip to main content
Participating Frequently
December 18, 2007
Question

[Mac CS3] Accessing boss classes and global variables

  • December 18, 2007
  • 9 replies
  • 691 views
I am use to programming with illustrator, but InDesign has a completely different model.
I am simply trying to create a global variable that i would be able to access from anywhere, but not sure how to go about it.
I created my own class and have it initialize and such, but I have no idea how to access the new class I created.
Any help would be much appreciated!
This topic has been closed for replies.

9 replies

Inspiring
December 20, 2007
You are welcome.
Known Participant
December 20, 2007
Thanks Peter
Ur Post Helps me a lot

Thanks To All
Participating Frequently
December 19, 2007
Thanks solved my problem right away.
I needed that extern declaration!
Inspiring
December 19, 2007
I do not know what happened there but let's expand my example with some more C/C++ constructs:

MyClass.h

#ifndef MYCLASS_H
#define MYCLASS_H

class MyClass {
public:
void HelloWorld();
};

extern MyClass gMyObject;

#endif /* MYCLASS_H */


MyClass.cpp

#ifndef MYCLASS_H
#include MyClass.h"
#endif

MyClass gMyObject;

MyClass::HelloWorld()
{
...
}


MyWork1.cpp

#ifndef MYCLASS_H
#include MyClass.h"
#endif

void MyWork1()
{
gMyObject.HelloWorld();
}


MyWork2.cpp

#ifndef MYCLASS_H
#include MyClass.h"
#endif

void MyWork2()
{
gMyObject.HelloWorld();
}


Also, make sure all my .cpp files are added to your project or makefile so they can be compiled and linked properly.

Good luck!
Known Participant
December 19, 2007
Hi Ian,
Ya that is a simple C++ stuff.But why It is giving Multiple declaration error i dont know i hav declared same as peter told.
ya but when i set

b solution explorer->properties->linker->commandline and set /FORCE:MULTIPLE

i am not getting any errors.

Hi Andres,
I defined all my class same as peter sample i defined

b extern MyClass gMyObject; in MyClass.h

and

b MyClass gMyObject; in MyClass.cpp

but when i iam including (#include "MyClass.h" ) in multiple files i am getting that error multiple declaration found

But it is working fine when i am including it at only one place.why?

Thanks u All.
Participating Frequently
December 19, 2007
You are probably defining gMyObject in "MyClass.h" instead of declaring it. You probably have a line like:
MyClass gMyObject;
instead of:
extern MyClass gMyObject;

The difference is that the first line creates a new "gMyObject" each time you include "MyClass.h" in another .cpp file, the second only **declares** that that variable exists. You need to use the second form in the ".h" file.

Additionally, you need to use the first form in exactly one .cpp file that includes the .h file, so that the variable is actually created. This is really basic C/C++ stuff.

Regards, Eugenio Andres - http://www.expertosinformaticos.com
Inspiring
December 19, 2007
You are can work in a standard C++ way for global variables - a plugin is effectively a .dll (not sure how it would work between plugins though). Your link error is a genuine link error not caused by the CS3/Windows environment - probably something in MyClass.h.

Ian
Known Participant
December 19, 2007
Hi Peter,
I am also trying for global in Indesign CS3[WIN] but if i am using globals in more than one class in my application like

b MyWork.cpp

#include "MyClass.h"

gMyObject.HelloWorld();

b MyWork1.cpp

#include "MyClass.h"

gMyObject.HelloWorld();

it is giving errorLNK2005:that it is already defined .multiple declaration found

how to get out from that error can u help me.i am working on Indesign CS3 windows
Inspiring
December 18, 2007
If you are referring "global variable" as in C/C++ sense, the following works for me:

MyClass.h

class MyClass {
public:
void HelloWorld();
};

extern MyClass gMyObject;

MyClass.cpp

MyClass gMyObject;

MyClass::HellWorld() {
.....
}

MyWork.cpp

#include "MyClass.h"

gMyObject.HelloWorld();