c++ - Force GCC not to optimize away an unused variable? -
One of the namespaces in my program provides an "engine" that is spread between two files; the other "command" Uses to follow different orders. All initial "engine" is done on the side, which includes caching parameters from the setup library.
Therefore, with engine.cpp
:
#include & lt; Stdio.h & gt; #include "ns.h" name space mines {unsigned character variable = 0; Zero init () {variable = 5; Printf ("Init: var =% d \ n", variable); } Zero handler () {// printf ("handler: var =% d \ n", variable); }}
commands.cpp
Used on scale.
#include & lt; Stdio.h & gt; #include "ns.h" name space mines {extern four unsigned char; Zero command () {printf ("command: var =% d \ n", variable); }}
After compilation and adding, I get:
init: var = 5
order: var = 1
Now, if I want to delete the printf () in handler ()
:
engine: var = 5
command : Var = 5 / handler: var = 5
In order to optimize this kind of GCC, it will not be "correct" in ways that it will extract from another file
Get the right value? Preferably, for the case, preferably without reducing main.h
and ns.h for
-O
level?
:)
#include "ns.h" int main (int argc, char ** argv) {myNS :: init (); MyNS :: Order (); MyNS :: Handler (); Return 0; }
namespace mines {zero} (); Zero order (); Zero handler (); }
This at least test case does not display this particular behavior; It appears that doing so requires a situation in very complex environment ...
Eh .. The solution was quite trivial.
I exchanged the definition of places and variables of the announcement.
engine.cpp
:
extern four unsigned char;
In this way, the compiler has no doubt about the existence of this variable, while compiling the current EDIT: Now I have discovered another specialty, depending on the value it has written. There is a section of code in question: The variable is near the location where multiple arrays are started with the memset, the query is variable If you move an assignment from line 7 and keep it after 1, 2 or 3 rows, then it receives the correct value command.cpp
:
Unsigned four variables = 0;
command
and engine
Need to reach the frequency, it can not just make a temporary look at the spot.
1: varso = SharedObject :: instance () -> Varso; 2: mammatet (dematmat, 0, saiff (atom)); 3: Mamet (GRmmap, 0x FF, Psychof (GRmmap)); 4: memset (GRDP, false, size (GRDP)); 5: Mementat (and Stan, 0, Psychof (Stan)); 6: stan.SOTUstage = 1; 7: PR_SOTU = varso- & gt; NRPSOUTU;
PR_SOTU
(obtained from uppercase macro time , And since it works in a very similar context with many other macros of acting, there is a possibility of it being this way too) 5
. This value placed after row 4 becomes 18
and anything below, and value is 1
Comments
Post a Comment