将curl-7.16.2.tar.bz2解压到某目录,例如C:\curl,进入C:\curl\lib目录。
将zlib123-dll.zip解压到某目录,例如C:\zlib
设Openssl的目录为C:\openssl
进入Visual Studio 2005命令提示,进入C:\curl\lib
编译Debug版本。
set CFG=debug-dll-ssl-dll-zlib-dll
set OPENSSL_PATH=C:/openssl
set ZLIB_PATH=C:/zlib/include
nmake -f Makefile.vc8
其输出:libcurld_imp.lib, libcurld.dll
编译Release版本。
set CFG=release-dll-ssl-dll-zlib-dll
set OPENSSL_PATH=C:/openssl
set ZLIB_PATH=C:/zlib/include
nmake -f Makefile.vc8
其输出:libcurl_imp.lib, libcurl.dll
如果需要编译其他版本,可查看设定相应的CFG 参数即可。
5.编译curlpp
将curlpp-0.7.0.tar.gz解压到某目录,例如C:\curlpp\curlpp
set CFG=release
nmake -f Makefile.msvc
如果需要编译其他版本,可查看设定相应的CFG 参数即可。
需要注意的是可能需要对原文件进行一定的修改。
一种方案是:
修改Makefile.msvc中LIBCURL_PATH
修改dllfct.h关于CURLPPAPI的宏定义
#define CURLPPAPI
6.测试
该程序是备份CUBlog程序的C++版,需要boost库
|
#include <iostream> #include <string> #include <queue>
#include <boost/lexical_cast.hpp> #include <boost/regex.hpp> #include <boost/tuple/tuple.hpp>
#include <curlpp/cURLpp.hpp> #include <curlpp/Easy.hpp> #include <curlpp/Options.hpp> #include <curlpp/Exception.hpp>
#pragma comment(lib,"libcurlpp.lib") #pragma comment(lib,"libcurl_imp.lib")
using namespace std;
#define MAX_FILE_LENGTH 20000
class WriterMemoryClass { public: // Helper Class for reading result from remote host WriterMemoryClass(){ this->m_pBuffer = NULL; this->m_pBuffer = (char*) malloc(MAX_FILE_LENGTH * sizeof(char)); this->m_Size = 0; } ~WriterMemoryClass() { if (this->m_pBuffer) free(this->m_pBuffer); } void* Realloc(void* ptr, size_t size) { if(ptr) return realloc(ptr, size); else return malloc(size); } // Callback must be declared static, otherwise it won't link... size_t WriteMemoryCallback(char* ptr, size_t size, size_t nmemb) { // Calculate the real size of the incoming buffer size_t realsize = size * nmemb; // (Re)Allocate memory for the buffer m_pBuffer = (char*) Realloc(m_pBuffer, m_Size + realsize); // Test if Buffer is initialized correctly & copy memory if (m_pBuffer == NULL) { realsize = 0; } memcpy(&(m_pBuffer[m_Size]), ptr, realsize); m_Size += realsize; // return the real size of the buffer... return realsize; } // Public member vars char* m_pBuffer; size_t m_Size; };
int main(int argc,char * argv[]) { string list_base="http://blog.chinaunix.net/u/8780/article.php?frmid=0&page="; string art_base="http://blog.chinaunix.net/u/8780/showart.php?id="; boost::regex rexp("<a href=\"showart_([0-9]{6}).*?><font.*?><b>(.*?)</b></font></a>"); boost::regex fnp("[/\\:*?\"<>]"); typedef boost::tuple<string,string> element; queue<element> q; bool save=true; if(argc==1) save=false; try { cURLpp::Cleanup cleaner; cURLpp::Easy request; cURLpp::Easy handler; int i=0; bool cond=true; while(cond) { i++; cond=false; string list_url=list_base+boost::lexical_cast<string>(i); cerr<<list_url<<endl; // Get the content WriterMemoryClass mWriterChunk; // Set the writer callback to enable cURL to write result in a memory area cURLpp::Types::WriteFunctionFunctor functor(&mWriterChunk,&WriterMemoryClass::WriteMemoryCallback); cURLpp::Options::WriteFunction *test = new cURLpp::Options::WriteFunction(functor); request.setOpt(test); // Setting the URL to retrive. request.setOpt(new cURLpp::Options::Url(list_url)); //request.setOpt(new cURLpp::Options::Verbose(true)); request.perform(); //cout<<mWriterChunk.m_pBuffer<<endl; // Get the id and article name pairs string s=string(mWriterChunk.m_pBuffer); string::const_iterator it=s.begin(); string::const_iterator end=s.end(); boost::smatch m; while(boost::regex_search(it,end,m,rexp)) { cond=true; //cout<<m[1].str()<<" "<<m[2].str()<<endl; q.push(element(m[1].str(),m[2].str())); it=m[0].second; } } // Backup cerr<<"There are "<<i-1<<" Pages"<<endl; cout<<"<html>\n<head>"<<endl; cout<<"<title>Linxh's CUBlog List</title>"<<endl; cout<<"</head>\n<body>"<<endl; cout<<"<font face=\"Arial, Helvetica\" size=4>"<<endl; while(!q.empty()) { element e=q.front(); q.pop(); string art_url=art_base+e.get<0>(); string fname=boost::tuples::get<1>(e); cout<<"<a target=\"_blank\" href=\""<<art_url<<"\">"<<fname<<"</a><br>"<<endl; if(save) { fname=boost::regex_replace(fname,fnp,"_"); fname+=".html"; cerr<<"Saving "<<art_url<<" as file "<<fname.c_str()<<endl; FILE * fp=fopen(fname.c_str(),"wb"); if(fp==NULL) { cerr<<"Could not open file "<<fname.c_str()<<endl; cerr<<"Skipping"<<endl; continue; } cURLpp::OptionTrait< void *, CURLOPT_WRITEDATA > myData(fp); handler.setOpt(myData); // Setting the URL to retrive.
handler.setOpt(new cURLpp::Options::Url(art_url)); //request.setOpt(new cURLpp::Options::Verbose(true)); handler.perform(); } } cout<<"</font>"<<endl; cout<<"</body>\n</html>"<<endl; } catch ( cURLpp::LogicError & e ) { std::cout << e.what() << std::endl; } catch ( cURLpp::RuntimeError & e ) { std::cout << e.what() << std::endl; }
return 0; }
|
一共有 8 条评论
QQ524103841
我自己写的一个类,由于回调函数必须为静态成员函数,但是不能访问类成员变量,是个很大问题。所以请教一下博主。
QQ524103841
#ifndef __DLLFCT__H_
#define __DLLFCT__H_
/*
* Decorate exportable functions for Win32 DLL linking.
* This avoids using a .def file for building libcurl.dll.
*/
#if (defined(WIN32) || defined(_WIN32)) && !defined(CURLPP_STATICLIB)
//#if defined(BUILDING_CURLPP)
//#define CURLPPAPI __declspec(dllexport)
//#else
//#define CURLPPAPI __declspec(dllimport)
//#endif
//#else
#define CURLPPAPI
#endif
#endif
我依照着步骤,进行到了以下位置:
1,修改Makefile.msvc中LIBCURL_PATH
2,修改dllfct.h关于CURLPPAPI的宏定义#define CURLPPAPI
第一步我已经修改,第二步不太清楚具体如何修改?还请博主赐教!
如果能发邮件到kevenqu@gmail.com,将感激不尽!提前说声谢谢!
屈威