linux curl編譯錯誤:curlrules.h:143:41: error: size of array ‘__curl_rule_01__’ is negative解決辦法

libcurl主要功能就是用不同的協議連接和溝通不同的服務器~也就是相當封裝了的sockPHP 支持libcurl(允許你用不同的協議連接和溝通不同的服務器)。, libcurl當前支持http, https, ftp, gopher, telnet, dict, file, 和ldap 協議。libcurl同樣支持HTTPS證書授權,HTTP POST, HTTP PUT, FTP 上傳(當然你也可以使用PHP的ftp擴展), HTTP基本表單上傳,代理,cookies,和用戶認證

編譯配置:

正常情況下,我們不需要強制配置curlib的編譯方式(32位或64位),配置如下:

./configure --disable-shared --enable-static --without-libidn --without-ssl -without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps -disable-ipv6  (編譯選項可自行配置)

make 

make install


但是如果在64位編譯機上編譯的curlib庫,在32位機器上使用爲出現錯誤:

curlrules.h:143:41: error: size of array ‘__curl_rule_01__’ is negative
     [CurlchkszEQ(long, CURL_SIZEOF_LONG)];
                                         ^
curlrules.h:153:53: error: size of array ‘__curl_rule_02__’ is negative

     [CurlchkszEQ(curl_off_t, CURL_SIZEOF_CURL_OFF_T)];


此時,就需要強制使用32位方式進行編譯,編譯腳本改爲如下:

CFLAGS='-m32' CPPFLAGS='-m32' ./configure --disable-shared --enable-static --without-libidn --without-ssl -without-librtmp --without-gnutls --without-nss --without-libssh2 --without-zlib --without-winidn --disable-rtsp --disable-ldap --disable-ldaps -disable-ipv6  (編譯選項可自行配置)

make 

make install


lubcurl的使用如下:

curlhttpclient.h

#ifndef _CURL_H
#define _CURL_H


#include "curl/curl.h" 
#include <iostream>
using namespace std;


class CUrlHttpClient 
{
public:


    CUrlHttpClient(){}
    ~CUrlHttpClient(){}
    /*
功    能  Curl POST請求
    參    數  strURL                輸入參數,請求的Url地址,如:http://www.baidu.com 
                strParam              輸入參數,Post請求參數,使用Json格式
        strResponse 輸出參數,返回的內容
                retCode 輸出參數,返回的狀態碼
    返 回 值  HTTP請求狀態碼
*/
    CURLcode Post(string strURl, string strParam, string & strResponse, long & retCode);


    /*
功    能  CURL GET請求
參    數  strURL                輸入參數,請求的Url地址,如:http://www.baidu.com 
     strResponse 輸出參數,返回的內容
     retCode 輸出參數,返回的狀態碼
返 回 值  HTTP請求狀態碼
*/
    CURLcode Get(const string & strUrl, string & strResponse, long & retCode);


/*
功    能  請求回覆信息處理函數
參    數  pBuffer               服務器回覆信息字符串
                     size 獲取數據的單字節數【1】
                     count size的個數
                    pParam 預置的配置參數
    返 回 值  服務器消息的字節數
    說    明  
pParam爲在發送數據請求時配置的參數,通常是文件指針,或字符串地址
這裏使用字符處,保存服務器返回數據
*/
    static size_t onReqReply(void *pBuffer, size_t size, size_t count, void *pParam);



static int onDebug(CURL *pcurl, curl_infotype itype, char * pData, size_t size, void *);  
private:
static  bool m_bDebug; 
public:
static  void SetDebug(bool bDebug);
};


#endif


curlhttpclient.cpp


#include "curlhttpclient.h"


CURLcode CUrlHttpClient::Post(string strURl, string strParam, string &strResponse, long & retCode)
{
// 初始化 CURL 全局變量,並分配全局資源
curl_global_init(CURL_GLOBAL_ALL);


CURLcode returnCode;


/* 初始化CURL,並獲取句柄 */
CURL * curl = curl_easy_init();  
if(NULL == curl)  
{
return returnCode;  
}


if(m_bDebug)  
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // 配置非0,表示輸出調試信息 
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, &onDebug); // 打印完整的調試信息
}


/* 配置URL */
curl_easy_setopt(curl, CURLOPT_URL, strURl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1); // 配置非0,表示發送POST請求


/* 配置HTTP請求 header*/
curl_slist *headers = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8");  
headers = curl_slist_append(headers, "Accept:application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); 


/* POST請求信息配置 */ 
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strParam.c_str()); // 設置POST請求參數
curl_easy_setopt(curl, CURLOPT_HEADER, 0); // 配置爲0,表示返回的內容裏不包含http header
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onReqReply); // 配置返回數據時執行的回調函數,可對返回數據進行處理
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);    // 設置傳遞給回調函數的第4個參數
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT_MS, 2000);  
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);  


/* 執行POST請求,並獲取返回值*/ 
returnCode = curl_easy_perform(curl);  


/* 獲取狀態碼 */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retCode); 


curl_slist_free_all(headers);  


/* 釋放資源 */
curl_easy_cleanup(curl);


/* 釋放全局資源 */
curl_global_cleanup(); 

return returnCode;  
}


CURLcode CUrlHttpClient::Get(const string & strUrl, string & strResponse, long & retCode)
{
// 初始化 CURL 全局變量,並分配全局資源
curl_global_init(CURL_GLOBAL_ALL);


CURLcode returnCode;


/* 初始化CURL,並獲取句柄 */
CURL * curl = curl_easy_init();  
if(NULL == curl)  
{  
return returnCode;  
}


if(m_bDebug)  
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // 配置非0,表示輸出調試信息 
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, onDebug); // 打印完整的調試信息
}


/* 配置URL */
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());

curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);  
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, onReqReply); // 配置返回數據時執行的回調函數,可對返回數據進行處理
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);    // 設置傳遞給回調函數的第4個參數
    /** 
    * 當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。 
    * 如果不設置這個選項,libcurl將會發信號打斷這個wait從而導致程序退出。 
    */  
    curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
    curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 6);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);


/* 執行POST請求,並獲取返回值*/ 
returnCode = curl_easy_perform(curl);  


/* 獲取狀態碼 */
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &retCode); 


/* 釋放資源 */
curl_easy_cleanup(curl);


/* 釋放全局資源 */
curl_global_cleanup(); 


return returnCode;  
}


 size_t CUrlHttpClient::onReqReply(void *pBuffer, size_t size, size_t count, void *lpVoid)
 {  
 string* str = dynamic_cast<string*>((string *)lpVoid);  
 if( NULL == str || NULL == pBuffer )  
 {  
 return -1;  
 }


 char* pData = (char*)pBuffer;  
 cout << "[" << pBuffer << ']';  
 str->append(pData, size * count);
 
 return size * count;  
  }


 int CUrlHttpClient::onDebug(CURL *pcurl, curl_infotype itype, char * pData, size_t size, void *)  
 {  
if(itype == CURLINFO_TEXT) 
{  
cout << "[TEXT]:" << pData;  
}  
else if(itype == CURLINFO_HEADER_IN)  
{  
cout << "[HEADER_IN]:" << pData;  
}  
else if(itype == CURLINFO_HEADER_OUT)  
{  
cout << "[HEADER_OUT]:" << pData;  
}  
else if(itype == CURLINFO_DATA_IN)  
{  
cout << "[DATA_IN]:" << pData;  
}  
else if(itype == CURLINFO_DATA_OUT)  
{  
cout << "[DATA_OUT]:" << pData;  
}  
return 0;  
 }


 bool CUrlHttpClient::m_bDebug = false;


 void CUrlHttpClient::SetDebug(bool bDebug)
 {
m_bDebug = bDebug;  
 }


使用:

        s8 achUrl[TP_REC_FILE_LEN + 1];
string strResponse;
long retCode;
sprintf(achUrl, "http://%s/%s?ssoToken=%s", tSSOServerInfo.achSSOServer, SSO_LOGIN_OUT, strToken.c_str());

m_cUrlHttpClient.Post(achUrl, "", strResponse, retCode);

strResponse 是網絡訪問的返回內容,retCode是網絡請求返回碼


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章