LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

global variable not active

Hello again,

Sorry if this another "newbie" question.

I have inherited a project file and am running into difficulties debugging. The programmer declared a global variable:

int CommPort;

Within a function the code attempts to assign the global a variable:

int function(int comport)
{
.
.
.
Commport = comport; //call this line 161
.
.
.
}

However, before and/or after I execute line 161, the variable is listed as "not active" when I view its value in the variable viewer or hold the mouse over "Commport".

Can somebody explain why this might be happening? I can't seem to force Commport to take the value of comport. Even before line 161 I expect Commport to be 0 due to the default global variable initialization made by the compiler. Does that sound correct?
0 Kudos
Message 1 of 9
(4,319 Views)
C is case sensitive.
You state that CommPort is declared as a global variable.
Your line 161 assigns a value to Commport (different case: p vs. P).

If Commport is just a typo in your post (and not your actual code), where and how do you declare CommPort as a global?
You're right that if CommPort is a global, it will be initialized to 0 and will be accessible anywhere.
0 Kudos
Message 2 of 9
(4,297 Views)
Indeed, you have found a typo in my post. "CommPort" is used throughout the code as the proper case and spelling. So let's disregard case mismatch as the problem at this point.

How much information are you looking for regarding the declaration of this variable? CommPort is declared as an int before any of the function code in its .c file, let's call it "comm.c", within the project. I should also mention that there is no "main" call within comm.c. comm.c is merely a collection of functions with some global declarations at the top of the page. The functions within comm.c are called from other files within the project.

comm.c is public within my organization and presumed to be functional for other users.
0 Kudos
Message 3 of 9
(4,290 Views)
> comm.c is merely a collection of functions with some global declarations at the top of the page. The functions within comm.c are called from other files within the project.

there is your problem - the 'other' files don't see the variable CommPort, only the functions in comm.c
you should declare CommPort in an extra header file comm.h and then
#include "comm.h"
in your other files.
CommPort should then be visible.
--
Once the game is over, the king and the pawn go back into the same box.
0 Kudos
Message 4 of 9
(4,288 Views)
The alternative to putting the variable declaration in a header file is to reference the variable as an external in all of the modules that use it, for instance:

external int CommPort;

This tells the compiler and linker that the variable is declared in another module and allows it to tie them all together at link time.

Either way will work but the problem with the header file approach occurs when it is included in multiple files in the project. The compiler will complain about the variable being redefined unless you put in some preprocessor directives to ensure that the header is included only once. Essnetially you have to do something like this:

#ifndef _SOME_SYMBOL_
#define _SOME_SYMBOL_

// Include your header code here

#endif
Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
0 Kudos
Message 5 of 9
(4,268 Views)
Does your response about declaring the extra .h header hold even if I only want to access CommPort exclusively within comm.c?

For clarity here is the general layout of my project (with many many lines of code and declarations omitted for simplicity) using aliased file names:

/***************************************/
/* main.c file */
main()
{
function_a1();
}
/* end main.c */
/***************************************/

/***************************************/
/* functions_a.c */
function_a1()
{
int rs232 = 7;
function_b1(rs232);
}
/* end functions_a.c */
/***************************************/

/***************************************/
/* functions_b.c */
int CommPort; /* global */

function_b1(int comport)
{
CommPort = comport; //call this line 161
function_b2();
}

function_b2()
{
flushoutQ(CommPort);
flushinQ(CommPort);
}

/* end functions_b.c */
/***************************************/


The execution of line 161 Commport = comport is where I see the "not active" message when I'm looking at CommPort's value. There is no change to value before or after execution of that line. The project compiles and everything runs without error.

Further, in function_b2, the flush functions execute without error even though CommPort is "not active". That seems mysterious to me.

The project compiles, but should it? Should there be errors (I don't see any) after executing lines containing the CommPort global? What does "not active" mean? I couldn't find a definition in the help file. How do I make CommPort "active"?
0 Kudos
Message 6 of 9
(4,267 Views)
I think the problem is really that CommPort, defined in functions_b.c
is not 'visible' in functions_a.c, from where function_b1 is called.
the compiler, however, will not complain, as CommPort is defined during compilation of functions_b.c
you should always try to make global variables visible to ALL affected modules by
defining them in header files which are then included, or use the proposed 'external' approach.
hope this helps.
--
Once the game is over, the king and the pawn go back into the same box.
0 Kudos
Message 7 of 9
(4,247 Views)
Thank you for the continued support.

I tried declaring CommPort as an extern in the functions_a.c file, but this did not solve the problem.

Would it help if I initialized CommPort to some value in the global declaration statement or is that illegal?

I would like to now try the header file option, but I'm slightly confused about the earlier explanation:
#ifndef _SOME_SYMBOL_
#define _SOME_SYMBOL_

// Include your header code here

#endif

Do I substitute CommPort for _SOME_SYMBOL_ in the explanation above?

Do I substitute #include "comm.h" for the "// Include your header code here" line?
0 Kudos
Message 8 of 9
(4,240 Views)
No, the structure of the header file is designed to allow it to compile only once. This is a very common C idiom you really should be familiar with.

Substitute a name that makes sense for _SOME_SYMBOL_ For instance, some variation of the header file name is often used. You want to make it reasonably certain of being unique to the particular file. As an example, for a file named instrument_util.h I would generally use a symbol such as _INSTR_UTIL_ or something similar. Sprinkling underscore characters through the name helps in trying to ensure uniqueness, this technique is often used by the compiler itself to "decorate" symbol names in the symbol tables in object files (but that is getting way outside the scope of this discussion).

Now what these preprocessor directives do is simple.

The first one checks that the symbol is not defined. If it isn't, the block of code between the #ifndef and the #endif directives are processed and compiled. The second directive defines the symbol so that next time the pre-processor and compiler see this file, they skip over the code and don't try to process it again.

The reason for doing this is that any global variables, typedefs, structure definitions and union definitions that are in the file should only be executed once or you will get an error in compilation.

So if you wanted to declare your global variable in the header file, its definition would go between the #define and #endif directives.

I hope that helps.
Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
0 Kudos
Message 9 of 9
(4,233 Views)