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

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

用gcc编译错误,因为重定义。

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

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ cat const.h 

#ifndef CONST_H

#define CONST_H

const int g_const = 12;

#endif // #ifndef CONST_H

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ cat test.c 

#include <stdio.h>

#include "const.h"

#include "test.h"

void test(){

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

}

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ cat main.c 

#include <stdio.h>

#include "const.h"

#include "test.h"

int main(){

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

    test();

        return 0;

}

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ gcc main.c test.c

/tmp/ccOM9phK.o:(.rodata+0x0): multiple definition of `g_const'

/tmp/cciYjaBU.o:(.rodata+0x0): first defined here

collect2: error: ld returned 1 exit status

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const1_c$ g++ main.c test.c 

frank@userver:~/project/test/const-test/const1_c$ ./a.out 

[main.c] g_const(0x40061c,12)

[test.c] g_const(0x400638,12)

frank@userver:~/project/test/const-test/const1_c$ 

------------------------------------------------------------------------------------

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

用g++可以编译。

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

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

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

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

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ cat const.h 

#ifndef CONST_H

#define CONST_H


#include<iostream>

using std::cout;

using std::endl;


const int g_const = 10;

class TestType{

public:

    TestType(){}

    void Show()const{

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

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

    }

};


#endif // #ifndef CONST_H

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ cat test.h

#ifndef TEST_H

#define TEST_H

void test();

#endif // #ifndef TEST_H

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ cat test.cpp 

#include "test.h"

#include "const.h"


void test(){

    cout << __func__ << endl;

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

    TestType obj;

    obj.Show();

}

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ cat main.cpp 

#include "test.h"

#include "const.h"


int main(){

    cout << __func__ << endl;

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

    TestType obj;

    obj.Show();

    test();

}

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ g++ main.cpp test.cpp 

frank@userver:~/project/test/const-test/const3_cpp$ ./a.out 

main

g_const: (0x400bd8, 10)

this=0x7fffcddbd39f, g_const:(0x400bd8, 10)

test

g_const = (0x400c08, 10)

this=0x7fffcddbd37f, g_const:(0x400bd8, 10)

------------------------------------------------------------------------------------

frank@userver:~/project/test/const-test/const3_cpp$ g++ test.cpp main.cpp 

frank@userver:~/project/test/const-test/const3_cpp$ ./a.out 

main

g_const: (0x400c08, 10)

this=0x7fff46b4525f, g_const:(0x400bd8, 10)

test

g_const = (0x400bd8, 10)

this=0x7fff46b4523f, g_const:(0x400bd8, 10)

------------------------------------------------------------------------------------

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

------------------------------------------------------------------------------------