title: Xcode C++ Unit testing with global variable
body: <p>I've got a problem when unit testing my program. 
  The problem is simple but i'm not sure why this is not working.</p>
  
  <p>1 -> i build all my program <br>
  2 -> i build my unitTest<br>
  3 -> the test is running.</p>
  
  <p>All is ok when it is not about getting global data from the data segment. It seems as if the variable are not initialized / or simply found. So of course all my tests become wrong.</p>
  
  <p>My question is: 
  Is it totally wrong to build an executable, then running the test on it? Or should i must compile all my code + the unit test in the same time, and then running it? Or is it just a lack of SenTesting framework?</p>
  
  <p>I forgot to mention that this is a C++ const string. Dunno if that change something.</p>
  
  <p><em><strong></em>*EDIT***</strong> <br></p>
  
  <p>My assumption was wrong, but i still don't understand the magic beyond! Seems a C++ magic hoydi hoo?</p>
  
  <pre><code>char cstring[] = "***";
  std::string cppString = "***";
  NSString* nstring = @"***";
  
  - (void)testSync{
      STAssertNotNil(nstring, nil); // fine
      STAssertNotNil((id)strlen(bbb), nil); // fine
      STAssertNotNil((id)cppString.size(), nil); // failed
  }
  </code></pre>
  
  <p><em>EDIT 2</em>** </p>
  
  <p>Actually this is normal that the C++ is not initialized at this part of the code. If i do a nm on my executable, it appears that my C and Obj-C global are put into the dataSegment. I thought my C++ string was in the same case, but it is actually put into the bss segment. That's means it's uninitialized. The fact is the C++ compiler do some magic beyond and the C++ string is initialized after the main() call and act like if it were into the dataSegment.</p>
  
  <p>I didn't know that testSuit doesn't have main() call, so the C++ object are never initialized. There is some technique in order to call the .ctor before the testSuit. But i am too lazy too explain and it's some kind of topic. I have just replaced my C++ string with a simple char array, and it work perfectly since my value are now POD.</p>
  
  <p>By the way there is no devil in global variable if they are just read-only. ;)</p>