问题
I have a unit (WebFunctions.h) with the declaration
String RawURLAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~.-_";
This unit works well in the app. But when I add a component which also declares WebFunctions.h
, the initialization of RawURLAllowedChars
does not occurs (RawURLAllowedChars = NULL
).
To get the app back to work, plus remove the class declaration of the component is still necessary delete the WebFunctions.obj
file.
Note: Any declarations in .cpp
file, with or without a extern
declaration in .h
file, also are not working.
This is a bug in XE2 or I'm missing something? Thanks.
Obs.: Var declarations inside #ifndef .. #endif
回答1:
Do not initialize a global variable in a header file. Declare the variable as extern
in the .h file and then initialize it in the corresponding .cpp file, eg:
WebFunctions.h:
extern const String RawURLAllowedChars;
WebFunctions.cpp:
const String RawURLAllowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~.-_";
回答2:
This is more a workaround than a real solution... but...
Comment (in the .cpp file of main app) the line with #pragma link "Component", added with the component which uses WebFunctions.h (not initializing unit).
Delete all building files in Debug\Win32 folder.
Recompile the App.
来源:https://stackoverflow.com/questions/16095768/variables-in-c-builder-xe2-not-initializing-when-unit-was-called-by-component