about common variable

R

Rex_chaos

I have define a MACRO varible as a common variable that be refered by
several classes.

#ifndef COMMON_VAR
#define COMMON_VAR 1

....

#endif

Whenever I want to change the value of common_var, I only need to
redefine it at the beginning of the source code. However, I am going
to compile all the classes into a library. That is, my code must link
to the library before I can use the classes. I found that redefine
the COMMON_VAR will no longer work because the classes has already
been compiled into a static library. How can I overcome this problem?
 
W

WW

Rex_chaos said:
I have define a MACRO varible as a common variable that be refered by
several classes.

There is not such thing as a "marco variable". It is a macro.
#ifndef COMMON_VAR
#define COMMON_VAR 1

...

#endif

Whenever I want to change the value of common_var, I only need to
redefine it at the beginning of the source code. However, I am going
to compile all the classes into a library. That is, my code must link
to the library before I can use the classes. I found that redefine
the COMMON_VAR will no longer work because the classes has already
been compiled into a static library. How can I overcome this problem?

You will need to use something else, not a macro. What do you want to do
exactly?
 
V

Victor Bazarov

Rex_chaos said:
I have define a MACRO varible as a common variable that be refered by
several classes.

There is no such thing as "MACRO varible". It's a macro. Nothing
variable about it.
#ifndef COMMON_VAR
#define COMMON_VAR 1

...

#endif

Whenever I want to change the value of common_var, I only need to
redefine it at the beginning of the source code. However, I am going
to compile all the classes into a library. That is, my code must link
to the library before I can use the classes.

"Compile classes into a library" and "code must link to the library"
are two different concepts. They cannot be connected with "that is".
I found that redefine
the COMMON_VAR will no longer work because the classes has already
been compiled into a static library. How can I overcome this problem?

Drop a macro and use a variable. Make it 'extern' if you expect
the users of your library to be able to change its value.

Read about "extern" linkage specifier in your favourite C++ book.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top