extern
做用1:让编译器按C规则编译函数名和变量名(保持名称原样,c++因为能够重载因此名称先后会添加符号)
#ifdef __cplusplus
extern "C"
{
#endif
#ifdef __cplusplus
}
#endif
做用2:在头文件中 extern int a; 声明全局变量或函数。其余编译单元能够包含头文件后定义或使用,在头文件中最好不要写成 extern int a = 1;
static
extern和static不能同时修饰一个变量,static声明了全局变量后,该变量同时也被定义了。static修饰的全局变量的做用域只能是自己的编译单元。
定义static全局变量时,通常把它放在原文件中而不是头文件。
// test.h
#ifndef TEST1_H
#define TEST1_H
static char str[] = "123456";
void func1();
#endif
// test1.cpp
#include "test1.h"
void func1()
{
str[0] = 'a';
cout << str << endl;
}
// test2.cpp
#include "test.h"
void func2()
{
cout << str << endl;
}
// main.cpp
int main()
{
func1(); // "a23456"
func2(); // "123456" 内存中存在两份拷贝给两个编译单元使用
return 0;
}
uint8_t[] 转 QString
uint8_t DevName[32] = {0};
QString devNameStr = (char*)DevName;