头文件不宜定义变量原因解析
2011-09-26 12:46
288 查看
test-1.0使用#ifndef只是防止了头文件被重复包含(其实本例中只有一个头件,不会存在重复包含的问题),但是无法防止变量被重复定义。
view
plain
# vi test.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern i;
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
int i = 10;
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern char add1[];
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern char add2[];
extern i;
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
view
plain
# Makefile
-------------------------------
test: test.o test1.o test2.o
test1.o: test1.c
test2.o: test2.c
clean:
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
view
plain
# vi test.c
-------------------------------
#include <stdio.h>
#include "test.h"
int i = 10;
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
extern i;
extern char add1[];
extern char add2[];
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include <stdio.h>
#include "test.h"
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include <stdio.h>
#include "test.h"
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
个人认为解决此类问题有几种办法:
1.在.cpp里定义变量,在其他调用处使用extern
2.在头文件里使用宏定义
view
plain
# vi test.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern i;
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
int i = 10;
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern char add1[];
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include <stdio.h>
#include "test.h"
extern char add2[];
extern i;
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
view
plain
# Makefile
-------------------------------
test: test.o test1.o test2.o
test1.o: test1.c
test2.o: test2.c
clean:
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
view
plain
# vi test.c
-------------------------------
#include <stdio.h>
#include "test.h"
int i = 10;
char add1[] = "www.shellbox.cn\n";
char add2[] = "www.scriptbox.cn\n";
extern void test1();
extern void test2();
int main()
{
test1();
printf("ok\n");
test2();
printf("%d\n",i);
return 0;
}
# vi test.h
-------------------------------
#ifndef _TEST_H_
#define _TEST_H_
extern i;
extern char add1[];
extern char add2[];
void test1();
void test2();
#endif
# vi test1.c
-------------------------------
#include <stdio.h>
#include "test.h"
void test1()
{
printf(add1);
}
# vi test2.c
-------------------------------
#include <stdio.h>
#include "test.h"
void test2()
{
printf(add2);
for (; i > 0; i--)
printf("%d-", i);
}
个人认为解决此类问题有几种办法:
1.在.cpp里定义变量,在其他调用处使用extern
2.在头文件里使用宏定义
相关文章推荐
- 头文件不宜定义变量原因解析
- 头文件不宜定义变量原因解析
- 头文件不宜定义变量原因解析
- 头文件不宜定义变量原因解析
- 【C++】变量定义在.h头文件导致 multiple definition of 的解决方法和根本原因
- 把全局变量定义在.h文件中的引起连接错误的原因以及解决办法
- 头文件中的变量定义
- 读取环境变量并利进行文件的解析
- C++“靠近定义变量“之真正原因
- 防止变量重复定义、头文件重复包含、嵌套包含
- Caffe 中关于 LetNet-5 网络的定义文件 lenet.prototxt 解析
- React 使用fromidable 模块进行文件上传出现First argument must be a string 的解析错误的原因
- 不要在头文件中定义变量
- 在BaseWidget.js中定义的变量appConfig可以表示全局的文件config.json
- const定义的全局变量不能被其他文件访问,必须加extern 才能被访问吗?
- 关于头文件定义全局变量等问题
- 【C++】在头文件中定义全局变量的技巧
- 多个cpp文件如何定义全局变量与常量
- PHP文件的上传下载中,全局变量$_FILES的作用及解析