C\C++语言变量或函数 只在文件内,请使用static

文章讲述了在工程中遇到多重定义问题时,如何利用static关键字来避免。具体包括将不对外提供的函数和仅在本文件使用的全局变量声明为static,以及通过ConnectDevice和getDataFromDevice等函数的示例说明了静态函数的使用方法。
摘要由CSDN通过智能技术生成

 在一个工程里,我有多个工站,几个相同的处理,如果直接复制,会提示多重定义。可用static

1.在源文件中,不对外提供的函数,定义时都用static修饰,将该函数的链接属性限定在本文件;
2.函数命名时,在每个函数的名字前都加上库的名称,一般库的名字是唯一且不重合的;
3.全局变量如果只在本文件中使用,最好也使用static修饰;

BOOL getDataFromDevice()
{
	CString sn;	
	CString csImei1, csImei2;
	int ret = halGetSn(sn.GetBuffer(32));
	sn.ReleaseBuffer();
	if (ret != 0) { pWnd->ShowInfo(("获取设备sn 错误码 " + to_string(ret)).c_str());		return FALSE; }

	ret = getAllImei(csImei1, csImei2);
	if (ret != 0) { pWnd->ShowInfo(("读IMEI失败 错误码 " + to_string(ret)).c_str()); 		return FALSE;	}

	pWnd->GetDlgItem(IDC_EDIT_SNsn)->SetWindowText(sn);
	pWnd->GetDlgItem(IDC_EDIT_IMEI1)->SetWindowText(csImei1);
	pWnd->GetDlgItem(IDC_EDIT_IMEI2)->SetWindowText(csImei2);
	SetClipboard(sn + "\r\n" + csImei1 + "\r\n" + csImei2);
	return TRUE;
}

static BOOL ConnectDevice()
{
	for (int i = 0; i < 60; i++)
	{
		pWnd->displayDeviceState(i + 1);
		int ret = halConnectDevice();
		if (ret == 0)
		{
			pWnd->displayDeviceState(0);
			return TRUE;
		}
		else
		{
			logger.INFO_F("连接次数" + to_string(i + 1) + " halConnectDevice ret=" + to_string(ret) + " " + errArray[(abs(ret) >= Size(errArray) ? Size(errArray) - 1 : abs(ret))]);
			if (i % 3 == 0)
			{
				pipe_system("adb.exe devices\n"); //看看设备是否存在
				pipe_system("adb.exe kill-server\n");
				pipe_system("adb.exe shell am force-stop com.ftsafe.cit\n");
				pipe_system("adb.exe shell am force-stop com.ftpos.productiontoolandroid");
				pipe_system("adb.exe shell am startservice -n com.ftpos.productiontoolandroid/com.ftpos.productiontoolandroid.ServiceManager -a com.ftpos.productiontoolandroid");
			}
			else
			{
				Sleep(DELAY);
			}
		}
	}
	pWnd->displayDeviceState(-1);
	return FALSE;
}
BOOL myprocGetDeviceInfo()
{
	if (ConnectDevice())
	{
		return getDataFromDevice();
	}
	return FALSE;
}

static UINT procCommon(LPVOID  lParam)
{
	if (g_bThreadFlag) { pWnd->ShowInfo("There are unfinished tasks");		return -1; }
	g_bThreadFlag = TRUE;
	logger.INFO_F(FUN_LINE" start ************************************************************");
	DWORD start = GetTickCount();
	pWnd->m_staResult.SetWindowText("");
	pWnd->m_editDisplay.SetWindowText("");
	pWnd->GetDlgItem(IDC_EDIT_SNsn)->SetWindowText("");
	pWnd->GetDlgItem(IDC_EDIT_IMEI1)->SetWindowText("");
	pWnd->GetDlgItem(IDC_EDIT_IMEI2)->SetWindowText("");
	typedef BOOL(*pMethod)(); //定义一种函数指针类型
	pMethod method = (pMethod)lParam; //强制转换为该类型
	pWnd->ShowSetResult(method()); //调用
	pWnd->ShowInfo(("耗时:" + to_string(GetTickCount() - start) + "ms").c_str());
	logger.INFO_F(FUN_LINE" end ************************************************************");
	g_bThreadFlag = FALSE;
	return 0;
}
void PrintAllDlg::OnBnClickedButtonGetdata()
{
	AfxBeginThread(procCommon, myprocGetDeviceInfo);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值