我原来也碰到过这个问题,可以避免就是在PROJECT->SETTING->LINK->GATEGORY 选CUSTOMIZE ,然后再选FORCE FILE OUTPUT 或 regarding global variables, as others pointed out that they are not good, you should use them as less as possible. if you do think you have to use global variables, you can do like this: // put all global variables into a '.cpp' file, for example: 'my_global_variables.cpp' int my_global_int1; int my_global_int2; float my_global_float1; ... // then write a header file ('my_global_variables.h') like this: extern int my_global_int1; extern int my_global_int2; extern float my_global_float1; ... you include 'my_global_variables.h' in every file that needs the global variables. when you compile the code, remember to compile and link the 'my_global_variable.cpp' with other files
在头文件中包含变量声明,如: extern int mydata; 在一个源文件(.cpp)中进行定义: int mydata = 0; 为什么要使用全局变量呢?如果只是为了 Frame和View的通信可以定义成View的public变量呀!
直接将变量的定义加到StdAfx。h文件中,不会出问题,更不会出现 MainFrm.obj : error LNK2005: "int number1" (?number1@@3HA) already defined in StdAfx.obj 如此错误,我试过的!
andy_lau(阿虎):你说的方法我试过,也是不行,你可以给我发一个例子, D:\temp\my001\my.cpp(13) : fatal error C1010: unexpected end of file while looking for precompiled header directive Error executing cl.exe. my001.exe - 1 error(s), 0 warning(s) zdl(zx) :也不行。 那位大虾给我出个主意,我现在定义了好多extern,真的不爽。
my.h:中 #ifndef _MY_H_ #define _MY_H_ extern int mydata; extern CString str1; #endifmy.cpp中 int number1; CString str1;
lyoubing(飘在风中的鱼): 用上面方法会出现如下错误 D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive
阿虎:我的问题如下,看到您的解释,我试了一下 http://www.csdn.net/expert/topic/628/628673.xml?temp=.7219202编译时发现出现下面错误: D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive 是什么原因,你解释一下好吗?要不留下你的信箱,我把这个例子给你发过去,你帮忙调试一下,发给我你的邮箱 还有:remember to compile and link the 'my_global_variable.cpp' with other files 是什么意思,怎么做,我不懂
Linking...
MainFrm.obj : error LNK2005: "int number1" (?number1@@3HA) already defined in StdAfx.obj
MainFrm.obj : error LNK2005: "class CString str1" (?str1@@3VCString@@A) already defined in StdAfx.obj
my001View.obj : error LNK2005: "int number1" (?number1@@3HA) already defined in StdAfx.obj
my001View.obj : error LNK2005: "class CString str1" (?str1@@3VCString@@A) already defined in StdAfx.obj
my.h:
#ifndef _MY_H_
#define _MY_H_int number1;
CString str1;#endif
afxgetapp()-〉。。就行了
选CUSTOMIZE ,然后再选FORCE FILE OUTPUT
或
regarding global variables, as others pointed out that they are not good, you should use them as less as possible. if you do think you have to use global variables, you can do like this: // put all global variables into a '.cpp' file, for example: 'my_global_variables.cpp'
int my_global_int1;
int my_global_int2;
float my_global_float1;
...
// then write a header file ('my_global_variables.h') like this:
extern int my_global_int1;
extern int my_global_int2;
extern float my_global_float1;
... you include 'my_global_variables.h' in every file that needs the global variables. when you compile the code, remember to compile and link the 'my_global_variable.cpp' with other files
#ifndef mydata
#define mydata
...
数据定义
...
#endif
extern int mydata;
在一个源文件(.cpp)中进行定义:
int mydata = 0;
为什么要使用全局变量呢?如果只是为了 Frame和View的通信可以定义成View的public变量呀!
MainFrm.obj : error LNK2005: "int number1" (?number1@@3HA) already defined in StdAfx.obj
如此错误,我试过的!
D:\temp\my001\my.cpp(13) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.
my001.exe - 1 error(s), 0 warning(s)
zdl(zx) :也不行。
那位大虾给我出个主意,我现在定义了好多extern,真的不爽。
#ifndef _MY_H_
#define _MY_H_
extern int mydata;
extern CString str1;
#endifmy.cpp中
int number1;
CString str1;
用上面方法会出现如下错误
D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive
http://www.csdn.net/expert/topic/628/628673.xml?temp=.7219202编译时发现出现下面错误:
D:\temp\my001\my.cpp(3) : fatal error C1010: unexpected end of file while looking for precompiled header directive
是什么原因,你解释一下好吗?要不留下你的信箱,我把这个例子给你发过去,你帮忙调试一下,发给我你的邮箱
还有:remember to compile and link the 'my_global_variable.cpp' with other files 是什么意思,怎么做,我不懂