libcurl 的 std 字符串问题 - c++
我对 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)。
I'm pretty new to c++ and I'm using libcurl to make an http request and get back a string with the respond's content.
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;
}
after running the above code (with VS2005) I get this errors:
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)
its seems like its a problem with some libraries and I tried adding "msvcrtd.lib" and I still get the error above with additional new errors.
Answer:
I changed the Run Time Library from Multi-Threaded (/MT) to Multi-threaded Debug DLL (/MDd).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要下载并编译源代码才能获取 DLL/库文件。我在为 VS 2005 下载的二进制版本中也遇到了类似的调试和链接问题。确保在编译器选项中包含标头和库路径,并链接 libcurl.dll 等。只需将其放在工作目录或 system32 文件夹中即可。
You would need to download and compile the source code to get your DLLs/Library files. I had the similar debugging and linking problems with binary version I downloaded for VS 2005. Make sure you include the header and library paths in compiler options and to link the libcurl.dll etc. just put it in the working directory or system32 folder.
std::string 通常不应该是以二进制形式(对象、静态库或 DLL)分发的库的公共接口的一部分。但 libcurl 的设计相当智能,可能 std::string 支持是由包含文件提供的(没关系),该文件在调用库之前将内容转换为可移植格式。
我认为您只需要小心地将调试版本与 libcurl 的调试版本链接起来,并将发布版本与发行版本链接起来。否则,程序的一部分需要 msvcrt.lib,一部分需要 msvcrtd.lib,如果您尝试同时使用两者,它们会发生冲突。
编辑以响应提问者的评论:
编译/构建工具栏中有一个下拉组合框,可让您在调试和发布配置之间进行选择。
此外,项目属性中的链接器“附加输入”设置对于“调试”和“发布”可以具有不同的值。调试版本可能应该使用“libcurld.lib”,发布版本应该使用“libcurl.lib”,但并非每个人都遵循相同的约定。
如果您将 .lib 文件添加到项目中,而不是在链接选项中列出它,您仍然可以通过添加两个变体并适当设置“从构建中排除此文件”来完成此操作。但这看起来很难看,并且会让从事该项目的其他人感到困惑。我将使用项目属性中的链接器选项。
std::string generally should not be part of a public interface of a library distributed in binary form (object, static lib, or DLL). But libcurl is pretty intelligently designed, probably the std::string support is provided by an include file (that's ok) which converts things to a portable format before calling into the library.
I think you just need to be careful to link your debug builds with the debug version of libcurl, and your release builds with the release version. Otherwise part of your program wants msvcrt.lib and part wants msvcrtd.lib, and they conflict if you try to use both at once.
EDIT in response to the asker's comment:
There's a dropdown combo box in the compile/build toolbar, that lets you select between Debug and Release configurations.
Also, the Linker "Additional Inputs" setting in the project properties can have different values for Debug and Release. Probably the debug version should use "libcurld.lib" and the release version "libcurl.lib", but not everyone follows that same convention.
If you added the .lib file to your project instead of listing it in the link options, you can still do this by adding both variants and setting "Exclude this file from the build" appropriately. But this looks ugly and will confuse anyone else who works on the project. I would use the Linker options in the project properties.
这是您发出请求所需的代码...如您所见,您必须创建一个名为 WriteMemoryCallback 的静态方法,其编码如下:
该解决方案非常适合我,并将信息存储在块中。内存成员。
告诉我它对你是否有用!
here is the code that you need to make the request... as you could see, you must create a static method called WriteMemoryCallback which is coded like this:
This solution works perfectly for me, and stores the info inside chunk.memory member.
Tell me if it was useful for you!