能否在头文件中定义全局static变量?

一、在没有类定义的头文件中定义全局static变量g_static。

用gcc和g++都可以编译。但运行结果显示,在test.c和main.c中,变量的值相同,但地址不同,说明是两个变量。

frank@userver:~/project/test/static-test/static1_c$ cat static.h 

#ifndef STATIC_H

#define STATIC_H

static int g_static = 56;

#endif // #ifndef STATIC_H

frank@userver:~/project/test/static-test/static1_c$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H

frank@userver:~/project/test/static-test/static1_c$ cat test.c 

#include <stdio.h>

#include "static.h"

#include "test.h"

void test(){

    printf("[%s] g_static(%p,%u)\n", __FILE__, &g_static, g_static);

}

frank@userver:~/project/test/static-test/static1_c$ cat main.c 

#include <stdio.h>

#include "static.h"

#include "test.h"

int main(){

    printf("[%s] g_static(%p,%u)\n", __FILE__, &g_static, g_static);

    test();

        return 0;

}

frank@userver:~/project/test/static-test/static1_c$ g++ main.c test.c 

frank@userver:~/project/test/static-test/static1_c$ ./a.out 

[main.c] g_static(0x601040,56)

[test.c] g_static(0x601044,56)

frank@userver:~/project/test/static-test/static1_c$ gcc main.c test.c 

frank@userver:~/project/test/static-test/static1_c$ ./a.out 

[main.c] g_static(0x601040,56)

[test.c] g_static(0x601044,56)

二、在有类定义的头文件中定义全局static变量。

用g++可以编译。

在类的成员函数中,g_static的地址保持一致,说明在类的不同的对象中会访问到同一个全局static变量。

在类外部的函数中,运行结果与编译顺序有关。

在先编译的文件的函数中,g_static地址与类的成员函数一致,说明与类内部的g_static是同一个变量。。

但是在后编译的文件的函数中,g_static的地址不同,说明是另一个变量。


frank@userver:~/project/test/static-test/static1_cpp$ cat static.h 

#ifndef STATIC_H

#define STATIC_H


#include<iostream>

using std::cout;

using std::endl;


static int g_static = 10;

class TestType{

public:

    TestType(){}

    void Show()const{

        cout << "this=" << this << ", ";

        cout << "g_static:(" << &g_static << ", " << g_static << ")\n";

    }

};


#endif // #ifndef STATIC_H

frank@userver:~/project/test/static-test/static1_cpp$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H


frank@userver:~/project/test/static-test/static1_cpp$ cat test.cpp 

#include "test.h"

#include "static.h"


void test(){

    cout << __func__ << endl;

    cout << "g_static = (" << &g_static << ", " << g_static << ")\n";

    TestType obj;

    obj.Show();

}

frank@userver:~/project/test/static-test/static1_cpp$ cat main.cpp 

#include "test.h"

#include "static.h"


int main(){

    cout << __func__ << endl;

    cout << "g_static: (" << &g_static << ", " << g_static << ")\n";

    TestType obj;

    obj.Show();

    test();

}

frank@userver:~/project/test/static-test/static1_cpp$ g++ main.cpp test.cpp 

frank@userver:~/project/test/static-test/static1_cpp$ ./a.out 

main

g_static: (0x602078, 10)

this=0x7fff09eba08f, g_static:(0x602078, 10)

test

g_static = (0x60207c, 10)

this=0x7fff09eba05f, g_static:(0x602078, 10)

frank@userver:~/project/test/static-test/static1_cpp$ g++ test.cpp main.cpp 

frank@userver:~/project/test/static-test/static1_cpp$ ./a.out 

main

g_static: (0x60207c, 10)

this=0x7fffdad480ef, g_static:(0x602078, 10)

test

g_static = (0x602078, 10)

this=0x7fffdad480bf, g_static:(0x602078, 10)

frank@userver:~/project/test/static-test/static1_cpp$ 

三、结论:不要在头文件中定义全局static变量。