Friday, December 14, 2007

C++: Borland C++ Builder Linker Errors

I kept getting these linker errors for a wrapper class I was writing.

[Linker Error] Unresolved external 'Curlit::errorBuffer' referenced from C:\PROJECTS\LIBCURL\CURLIT.OBJ
[Linker Error] Unresolved external 'Curlit::buffer' referenced from C:\PROJECTS\LIBCURL\CURLIT.OBJ

Now I thought I was just getting random linker errors. But only after I played with things a bit did I realize it was because errorBuffer and buffer were declared static.

class Curlit
{
protected:
static char errorBuffer[CURL_ERROR_SIZE];
static string buffer;
static int writer(char *data, size_t size, size_t nmemb, string *buffer);
static string easycurl(string &url, bool post, string &pstring);

public:
Curlit();
~
Curlit();
static string post(string &url, std::map<string, string> &querystr);
static string get(string &url);
static string escape(string &param);
};

Once I realized it was because they were static, I was able to put the right words into google. I soon learned that static class members, must be redeclared outside the class definition as shown below. Once I added two lines of code, the linker errors went away.

class Curlit
{
protected:
static char errorBuffer[CURL_ERROR_SIZE];
static string buffer;
static int writer(char *data, size_t size, size_t nmemb, string *buffer);
static string easycurl(string &url, bool post, string &pstring);

public:
Curlit();
~
Curlit();
static string post(string &url, std::map<string, string> &querystr);
static string get(string &url);
static string escape(string &param);
};

char Curlit::errorBuffer[CURL_ERROR_SIZE];
string Curlit::buffer;

source(s): programmersheaven.com/mb/CandCPP/67811/67811/ReadMessage.aspx

No comments: