Windows下編譯curlpp

來源:互聯網
上載者:User

curlpp是libcurl的C++封裝,由於作者本身並不使用Visual Studio(可能並不貼切,但總之作者提供的vs工程並無法直接編譯通過並運行),所以在Windows下的構建並不是解壓後就可以編譯通過。我自己搗鼓了一個多小時,才發現原來是這麼回事。於是將編譯的過程和注意事項分享如下。

1.curl下載
從這個http://curl.haxx.se/download.html頁面下載,我下載的是http://curl.haxx.se/download/curl-7.20.1.tar.gz

2.curlpp下載
從http://curlpp.org/index.php/download這個頁面下載, 我下載的是http://curlpp.googlecode.com/files/curlpp-0.7.3.tar.gz

3.編譯curl
由於curlpp是對curl的封裝,他必須依賴於curl,所以必須先編譯curl。我的機器只安裝了vs2008,我的步驟如下:
001.直接用vs2008開啟curl-7.20.1/vc6curl.dsw,提示升級工程到vc9,點擊Finish完成升級,這樣你就有了一個vc6curl.sln
002.編譯所有工程配置,我這一點問題都沒有直接編譯通過了,這一步你可以得到的是:
curl-7.20.1/lib/DLL-Debug/
curl-7.20.1/lib/DLL-Release/
curl-7.20.1/lib/LIB-Debug/
curl-7.20.1/lib/LIB-Release/
這四個目錄,產生的是DLL還是Lib看目錄名就明白了。curlpp預設是用動態連結的方式使用curl,對應的目錄/lib/DLL-Debug和lib/DLL-Release中的lib。 

4.編譯curlpp
001.解壓代碼後,開啟curlpp-0.7.3/curlpp.VC8.vcproj,還是一樣提示升級工程到vc9,升級完成。
002. 直接編譯會有問題,提示無法找到curl/curl.h等等,我們需要做的事情是:設定$(LIBCURL_PATH)這個宏,把他的值設定成第3步的curl目錄。設定的方法是:添加一個系統內容變數或者添加一個VS使用者自訂宏,添加的方法可以參考這個:vs2005如何使用使用者自訂宏(User Macros)

003.設定好curl的路徑後你會發現還是提示你有幾個檔案找不到:
infos.cpp、option.cpp這兩個直接從工程去掉就可以了
LifetimeLibrary.cpp,PrivateMembers.cpp,這兩個檔案只是工程中的路徑錯了,改一下屬性中的引用路徑就可以了,當然要去掉也可以。(在vs的Solution Explorer中進行)

004.編譯會提示你找不到libcurl.lib,原因是工程設定中的lib引用目錄不對。
修改如下(這個路徑其實和你設定的LIBCURL_PATH)這個自訂環境變數所對應的curl目錄下的結構有關,按上面的步驟下來的話這裡需要修改下):
Project properties/Linker/General/ Additional  Library Directory:$(LIBCURL_PATH)/lib/DLL-Debug
Project properties/Linker/Input/Additional Dependencies: libcurld_imp.lib
 
下面是從README.win32中截取的一段相關說明
Linking with libcurl</p><p> All versions of this library need libcurl (c library) include files to compile and lib file to link successfully.<br /> To build curlpp you first have to set value of LIBCURL_PATH user macro to your installation of libcurl.<br /> This macro is used in the project file for VC9 to find include and lib files of libcurl.<br /> Steps to set user macro. Select View/Property Manager, double-click curlpp property sheet, go to<br /> Common Properties/User Macros/, double-click name of macro you want to modify, modify it and click OK.<br /> Filename of libcurl's lib file should be in the form libcurlX.lib where X denotes build category chosen for curlpp.<br /> (see Linking with RTL)<br /> If it has different name you have to change .lib filename in<br /> Project properties/Librarian/Additional Dependencies (when building static library)<br /> or<br /> Project properties/Linker/Input/Additional Dependencies (when building dynamic library).</p><p> Linking with static libcurl</p><p> If you are not going to use libcurl as a dll and you would like to include it into your library/executable by<br /> linking to static version of libcurl you have to define preprocessor symbol CURL_STATICLIB. You can do it in<br /> Project/Properties/C/C++/Preprocessor/Preprocessor Definitions.<br /> By default all StaticLib* configurations define this symbol. If for some reason you would like to build static<br /> curlpp library which links do dynamic libcurl library you have to remove CURL_STATICLIB define.  

 005.編譯時間還會出現一個錯誤:
1>c1xx : fatal error C1083: 無法開啟源檔案:“WIN32”: Permission denied
這個是因為工程設定中的include有非法的路徑(應該是boost),去掉就可以了
原來:"$(LIBCURL_PATH)/include";./;include/curlpp;include;"$(BOOST_PATH)"
改成:$(LIBCURL_PATH)/include";include/curlpp;include;

5.好了,編譯通過了:) 你可以得到的是:

DebugDynamic/curlpp.dll
ReleaseDynamic/curlpp.dll
DebugDynamic/curlpp.lib
DebugStatic/curlpp.lib
ReleaseDynamic/curlpp.lib
ReleaseStatic/curlpp.lib 

 
6.測試和確認
我需要用curlpp的example來測試下我的使用,但是使用時卻發現會有這樣的編譯錯誤:

1>------ Build started: Project: CUrlppDemo, Configuration: Debug Win32 ------
1>Compiling...
1>CUrlppDemo.cpp
1>d:/curlpp-0.7.3/include/curlpp/exception.hpp(49) : warning C4275: 非 dll 介面 class“std::runtime_error”用作 dll 介面 class“curlpp::RuntimeError”的基
1>        d:/program files/microsoft visual studio 9.0/vc/include/stdexcept(148) : 參見“std::runtime_error”的聲明
1>        d:/curlpp-0.7.3/include/curlpp/exception.hpp(48) : 參見“curlpp::RuntimeError”的聲明
1>d:/curlpp-0.7.3/include/curlpp/exception.hpp(70) : warning C4275: 非 dll 介面 class“std::logic_error”用作 dll 介面 class“curlpp::LogicError”的基
1>        d:/program files/microsoft visual studio 9.0/vc/include/stdexcept(21) : 參見“std::logic_error”的聲明
1>        d:/curlpp-0.7.3/include/curlpp/exception.hpp(69) : 參見“curlpp::LogicError”的聲明
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(183) : warning C4251: “curlpp::internal::CurlHandle::mWriteFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=size_t,
1>            TList=utilspp::tl::TypeList<char *,utilspp::tl::TypeList<size_t,utilspp::tl::TypeList<size_t,utilspp::NullType>>>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(184) : warning C4251: “curlpp::internal::CurlHandle::mHeaderFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=size_t,
1>            TList=utilspp::tl::TypeList<char *,utilspp::tl::TypeList<size_t,utilspp::tl::TypeList<size_t,utilspp::NullType>>>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(185) : warning C4251: “curlpp::internal::CurlHandle::mReadFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=size_t,
1>            TList=utilspp::tl::TypeList<char *,utilspp::tl::TypeList<size_t,utilspp::tl::TypeList<size_t,utilspp::NullType>>>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(186) : warning C4251: “curlpp::internal::CurlHandle::mProgressFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=int,
1>            TList=utilspp::tl::TypeList<double,utilspp::tl::TypeList<double,utilspp::tl::TypeList<double,utilspp::tl::TypeList<double,utilspp::NullType>>>>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(187) : warning C4251: “curlpp::internal::CurlHandle::mDebugFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=int,
1>            TList=utilspp::tl::TypeList<curl_infotype,utilspp::tl::TypeList<char *,utilspp::tl::TypeList<size_t,utilspp::NullType>>>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/curlhandle.hpp(188) : warning C4251: “curlpp::internal::CurlHandle::mSslFunctor”: class“utilspp::Functor<R,TList>”需要有 dll 介面由 class“curlpp::internal::CurlHandle”的用戶端使用
1>        with
1>        [
1>            R=CURLcode,
1>            TList=utilspp::tl::TypeList<void *,utilspp::NullType>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/optionlist.hpp(110) : warning C4251: “curlpp::internal::OptionList::mOptions”: class“std::map<_Kty,_Ty>”需要有 dll 介面由 class“curlpp::internal::OptionList”的用戶端使用
1>        with
1>        [
1>            _Kty=CURLoption,
1>            _Ty=curlpp::OptionBase *
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/slist.hpp(77) : warning C4251: “curlpp::internal::SList::mData”: class“std::list<_Ty>”需要有 dll 介面由 class“curlpp::internal::SList”的用戶端使用
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/form.hpp(91) : warning C4251: “curlpp::HttpPost::mForms”: class“std::list<_Ty>”需要有 dll 介面由 class“curlpp::HttpPost”的用戶端使用
1>        with
1>        [
1>            _Ty=utilspp::clone_ptr<curlpp::FormPart>
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/internal/optioncontainer.inl(39) : error C2491: “curlpp::internal::OptionContainer<OptionType>::OptionContainer”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optioncontainer.inl(45) : error C2491: “curlpp::internal::OptionContainer<OptionType>::OptionContainer”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optioncontainer.inl(53) : error C2491: “curlpp::internal::OptionContainer<OptionType>::setValue”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optioncontainer.inl(60) : error C2491: “curlpp::internal::OptionContainer<OptionType>::getValue”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optioncontainer.inl(67) : error C2491: “curlpp::internal::OptionContainer<OptionType>::getHandleOptionValue”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optionsetter.inl(36) : error C2491: “curlpp::internal::OptionSetter<OptionValueType,optionType>::setOpt”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optionsetter.inl(45) : error C2491: “curlpp::internal::OptionSetter<std::string,optionType>::setOpt”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/internal/optionsetter.inl(54) : error C2491: “curlpp::internal::OptionSetter<std::list<_Ty>,optionType>::setOpt”: 不允許 dllimport 函數 的定義
1>        with
1>        [
1>            _Ty=std::string
1>        ]
1>d:/curlpp-0.7.3/include/curlpp/option.inl(45) : error C2491: “curlpp::Option<OT>::Option”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(52) : error C2491: “curlpp::Option<OT>::Option”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(63) : error C2491: “curlpp::Option<OT>::Option”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(71) : error C2491: “curlpp::Option<OT>::~Option”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(86) : error C2491: “curlpp::Option<OT>::setValue”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(99) : error C2491: “curlpp::Option<OT>::updateMeToOption”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(107) : error C2491: “curlpp::Option<OT>::clear”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(117) : error C2491: “curlpp::Option<OT>::getValue”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(125) : error C2491: “curlpp::OptionTrait<OptionType,opt>::OptionTrait”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(131) : error C2491: “curlpp::OptionTrait<OptionType,opt>::OptionTrait”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(139) : error C2491: “curlpp::OptionTrait<OptionType,opt>::clone”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(152) : error C2491: “curlpp::OptionTrait<OptionType,opt>::updateHandleToMe”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(160) : error C2491: “curlpp::NoValueOptionTrait<option>::NoValueOptionTrait”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(168) : error C2491: “curlpp::NoValueOptionTrait<option>::clone”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(178) : error C2491: “curlpp::NotAvailableOptionTrait<OptionType>::NotAvailableOptionTrait”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(185) : error C2491: “curlpp::NotAvailableOptionTrait<OptionType>::NotAvailableOptionTrait”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(193) : error C2491: “curlpp::NotAvailableOptionTrait<OptionType>::clone”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/option.inl(201) : error C2491: “curlpp::NotAvailableOptionTrait<OptionType>::updateHandleToMe”: 不允許 dllimport 函數 的定義
1>d:/curlpp-0.7.3/include/curlpp/easy.hpp(154) : warning C4251: “curlpp::Easy::mCurl”: class“std::auto_ptr<_Ty>”需要有 dll 介面由 class“curlpp::Easy”的用戶端使用
最終我發現的原來問題出在一個宏定義上:
#define BUILDING_CURLPP

修改如下:
#define BUILDING_CURLPP// 在#include之前加上這個就可以了!! @debehe
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Options.hpp>

看下example的吧,開心ing

關於curlpp的問題還可以到這裡討論、尋求協助:
http://groups.google.com/group/curlpp 
關於編譯的問題還可以看看這裡的討論:http://news.gmane.org/gmane.comp.web.curl.curlpp.general

2010/4/17 22:52 搞定了之前的example編譯出錯的問題

The End

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.