日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

VS2010引用全局變量報錯:無法解析的外部符號

作者:Pisces_224 更新時間: 2022-02-27 編程語言

如題

很久不搞C++了,最近應工作要求,在VS2010下開發(fā)mfc客戶端程序。因為涉及到客戶端訪問服務端,必須通過賬戶登錄來確保安全性,所以需要客戶端登錄拿到服務端返回的token,然后客戶端后續(xù)所有url請求都要攜帶token去訪問。

這塊功能用的是httpclient 和 curl庫,網上資料很多,這里不做過多敘述,但也給出一個get/post請求函數的寫法:

Get()

// 向服務器發(fā)出get請求
int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{
	CURLcode res;
	CURL* curl = curl_easy_init();

	//打印Get命令
	//logprint(strUrl.c_str());

	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
	if(m_bDebug)
	{
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
	}
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);

	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, "Content-type: application/json;charset='utf-8'");
	// 轉換CString為const char*,作為httpheader一部分
	std::string cstrToken ="Authorization:" + GLOBAL_TOKEN;

	headers = curl_slist_append(headers, cstrToken.c_str());          請求頭添加token校驗身份
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
	/**
	* 當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。
	* 如果不設置這個選項,libcurl將會發(fā)信號打斷這個wait從而導致程序退出。
	*/
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//連接超時,這個數值如果設置太短可能導致數據請求不到就斷開了
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收數據時超時設置,如果10秒內數據未接收完,直接退出
	res = curl_easy_perform(curl);
	curl_easy_cleanup(curl);

	//打印輸出結果
//	logprint("httpclient raw std string data=",strResponse.c_str());
	return res;
}

Post()

// 向服務器發(fā)出post請求
int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse/*,bool bStructHttppost,struct curl_httppost* httppost*/)
{

	CURLcode res;
	CURL* curl = curl_easy_init();
	if(NULL == curl)
	{
		return CURLE_FAILED_INIT;
	}
	if(m_bDebug)
	{
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
		curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
	}
	curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
	curl_easy_setopt(curl, CURLOPT_POST, 1);
 // 以下是請求頭部分
	struct curl_slist *headers = NULL;
	headers = curl_slist_append(headers, "Content-type: application/json;charset='utf-8'");
	// 轉換CString為const char*,作為httpheader一部分
	std::string cstrToken ="Authorization:" + GLOBAL_TOKEN;
	
	headers = curl_slist_append(headers, cstrToken.c_str());          請求頭添加token校驗身份
	curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);


	curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());

	curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
	curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
	curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//連接超時,這個數值如果設置太短可能導致數據請求不到就斷開了
	curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);//接收數據時超時設置,如果10秒內數據未接收完,直接退出
	res = curl_easy_perform(curl);
	curl_easy_cleanup(curl);
	return res;	
}

全局引用變量

一開始,我定義了一個extern GLOBAL_TOKEN變量放在一個公共頭文件中,然后在需要引用的地方引用,但是報錯:無法解析的外部符號。經過一番查找資料,偶然看到一位老哥的解決方法:

打開類視圖管理器,找到 theApp這個變量,因為任何一個mfc程序都會有它,然后照貓畫虎,在該文件.cpp中定義好如int var1;,然后在該文件的頭文件末尾聲明extern int var1; 最后即可在任意地方引用該變量了。

原文鏈接:https://blog.csdn.net/qq_36256590/article/details/122987948

欄目分類
最近更新