头文件不宜定义变量原因解析

test-1.0使用#ifndef只是防止了头文件被重复包含(其实本例中只有一个头件,不会存在重复包含的问题),但是无法防止变量被重复定义

  1. # vi test.c  
  2. -------------------------------  
  3. #include <stdio.h>  
  4. #include "test.h"  
  5.   
  6. extern i;  
  7. extern void test1();  
  8. extern void test2();  
  9.   
  10. int main()  
  11. {  
  12.    test1();  
  13.    printf("ok\n");  
  14.    test2();  
  15.    printf("%d\n",i);  
  16.    return 0;  
  17. }  
  18.   
  19.   
  20. # vi test.h  
  21. -------------------------------  
  22. #ifndef _TEST_H_  
  23. #define _TEST_H_  
  24.   
  25. char add1[] = "www.shellbox.cn\n";  
  26. char add2[] = "www.scriptbox.cn\n";  
  27. int i = 10;  
  28. void test1();  
  29. void test2();  
  30.   
  31. #endif  
  32.   
  33.   
  34.   
  35. # vi test1.c  
  36. -------------------------------  
  37. #include <stdio.h>  
  38. #include "test.h"  
  39.   
  40. extern char add1[];  
  41.   
  42. void test1()  
  43. {  
  44.    printf(add1);  
  45. }  
  46.   
  47.   
  48.   
  49. # vi test2.c  
  50. -------------------------------  
  51. #include <stdio.h>  
  52. #include "test.h"  
  53.   
  54. extern char add2[];  
  55. extern i;  
  56.   
  57. void test2()  
  58. {  
  59.    printf(add2);  
  60.    for (; i > 0; i--)   
  61.        printf("%d-", i);  
  62. }  



 

  1. # Makefile  
  2. -------------------------------  
  3. test:    test.o test1.o test2.o  
  4. test1.o: test1.c  
  5. test2.o: test2.c  
  6. clean:  
  7.    rm test test.o test1.o test2.o  




错误:
test-1.0编译后会出现"multiple definition of"错误。

错误分析:
由于工程中的每个.c文件都是独立的解释的,即使头文件有
#ifndef _TEST_H_
#define _TEST_H_
....
#enfif
在其他文件中只要包含了global.h就会独立的解释,然后每个.c文件生成独立的标示符。在编译器链接时,就会将工程中所有的符号整合在一起,由于文件中有重名变量,于是就出现了重复定义的错误。

解决方法
.c文件中声明变量,然后建一个头文件(.h文件)在所有的变量声明前加上extern,注意这里不要对变量进行的初始化。然后在其他需要使用全局变量的.c文件中包含.h文件。编译器会为.c生成目标文件,然后链接时,如果该.c文件使用了全局变量,链接器就会链接到此.c文件 。






test-2.0

  1. # vi test.c  
  2. -------------------------------  
  3. #include <stdio.h>  
  4. #include "test.h"  
  5.   
  6. int i = 10;  
  7. char add1[] = "www.shellbox.cn\n";  
  8. char add2[] = "www.scriptbox.cn\n";  
  9. extern void test1();  
  10. extern void test2();  
  11.   
  12. int main()  
  13. {  
  14.    test1();  
  15.    printf("ok\n");  
  16.    test2();  
  17.    printf("%d\n",i);  
  18.    return 0;  
  19. }  
  20.   
  21.   
  22. # vi test.h  
  23. -------------------------------  
  24. #ifndef _TEST_H_  
  25. #define _TEST_H_  
  26.   
  27. extern i;  
  28. extern char add1[];  
  29. extern char add2[];  
  30.   
  31. void test1();  
  32. void test2();  
  33.   
  34. #endif  
  35.   
  36.   
  37.   
  38. # vi test1.c  
  39. -------------------------------  
  40. #include <stdio.h>  
  41. #include "test.h"  
  42.   
  43. void test1()  
  44. {  
  45.    printf(add1);  
  46. }  
  47.   
  48.   
  49. # vi test2.c  
  50. -------------------------------  
  51. #include <stdio.h>  
  52. #include "test.h"  
  53.   
  54. void test2()  
  55. {  
  56.    printf(add2);  
  57.    for (; i > 0; i--)   
  58.        printf("%d-", i);  
  59. }  


 个人认为解决此类问题有几种办法:

1.在.cpp里定义变量,在其他调用处使用extern

2.在头文件里使用宏定义

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值