Home >Backend Development >C++ >Why Am I Getting 'Undefined Symbols for Architecture i386' When Using Static Variables in C ?
Error: Undefined Symbols When Compiling with Static Library
When compiling C code in XCode, you may encounter the error message "Undefined symbols for architecture i386." This error typically occurs due to undefined static variables within the code.
Problem Details
In the provided code, the class Log has a static variable theString declared in the header file Log.h but is not defined in the implementation file Log.cpp. This results in the linker being unable to resolve the reference to the variable during compilation.
Solution
To resolve this error, you must define the static variable in the implementation file.
// Log.cpp #include "Log.h" #include <iostream> string Log::theString; // Define static here void Log::method(string arg) { theString = "hola"; cout << theString << endl; }
Additional Recommendation
It is also recommended to remove the line using namespace std; from the header file as it pollutes the global namespace with std wherever the header is included. Instead, you should explicitly specify the std namespace when using its objects.
// Log.h #include <iostream> #include <string> class Log { public: static void method(std::string arg); private: static std::string theString; };
The above is the detailed content of Why Am I Getting 'Undefined Symbols for Architecture i386' When Using Static Variables in C ?. For more information, please follow other related articles on the PHP Chinese website!