我是 C++ 的新手,我正在使用 libcurl 发出 http 请求并返回包含响应内容的字符串。
size_t write_to_string(void *ptr, size_t size, size_t count, void *stream) {
((std::string*)stream)->append((char*)ptr, 0, size*count);
return size*count;
}
int main(void) {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://www.browsarity.com/");
std::string response;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_to_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
// The "response" variable should now contain the contents of the HTTP response
}
return 0;
}
运行上面的代码后(使用 VS2005)我得到这个错误:
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __malloc_dbg referenced in function "void * __cdecl operator new(unsigned int,struct std::_DebugHeapTag_t const &,char *,int)" (??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(xdebug.obj) : error LNK2019: unresolved external symbol __free_dbg referenced in function "void __cdecl operator delete(void *,struct std::_DebugHeapTag_t const &,char *,int)" (??3@YAXPAXABU_DebugHeapTag_t@std@@PADH@Z)
1>libcpmtd.lib(stdthrow.obj) : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "void __cdecl std::_Debug_message(wchar_t const *,wchar_t const *,unsigned int)" (?_Debug_message@std@@YAXPB_W0I@Z)
这似乎是某些库的问题,我尝试添加“msvcrtd.lib”,但我仍然收到上述错误以及其他新错误。
回答: 我将运行时库从多线程 (/MT) 更改为多线程调试 DLL (/MDd)。
最佳答案
您需要下载并编译源代码以获取您的 DLL/库文件。我在为 VS 2005 下载的二进制版本中遇到了类似的调试和链接问题。确保在编译器选项中包含 header 和库路径,并链接 libcurl.dll 等。只需将其放在工作目录或 system32 文件夹中。
关于c++ - libcurl 的标准字符串问题 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2395300/