Troubleshooting
Problem
Attempts to compile your program with XL C/C++ compilers results in the static objects in the program not initialized to the right value.
Symptom
While compiling the following C++ program, you might assume that the objects from the module that is compiled first are initialized first. This assumption might lead to unexpected results.
X.h :
#include <string>
class CObjet {
public:
static const string STRINGX;
};
X.cpp:
#include <X.h>
using namespace std;
const string CObjet::STRINGX = "001";
Y.cpp:
#include <iostream>
#include <X.h>
const string STRINGY= CObjet::STRINGX;
int main () {
cout << "CObjet::STRINGX [" <<CObjet::STRINGX << "]" << endl;
cout << "STRINGY [" << STRINGY << "]" << endl;
return 0;
}
If you use the following commands to compile the files:
xlC -c -I./ X.cpp -o X.o
xlC -c -I./ Y.cpp -o Y.o
xlC -o binary X.o Y.o
You will get the following result when you run the executable file:
./binary
CObjet::STRINGX [001]
STRINGY []
From the result, you can see that STRINGY is not initialized to the value of STRINGX.
[{"Product":{"code":"SSGH3R","label":"XL C\/C++ for AIX"},"Business Unit":{"code":"BU048","label":"IBM Software"},"Component":"Compiler","Platform":[{"code":"PF002","label":"AIX"}],"Version":"10.1;11.1;12.1","Edition":"","Line of Business":{"code":"LOB73","label":"Power TPS"}}]
Log InLog in to view more of this document
This document has the abstract of a technical article that is available to authorized users once you have logged on. Please use Log in button above to access the full document. After log in, if you do not have the right authorization for this document, there will be instructions on what to do next.
Was this topic helpful?
Document Information
Modified date:
06 December 2018
UID
swg21615387