We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
i write a global.h file: #ifndef GLOBAL_H #define GLOBAL_H int nGlobal; #endif //file A.c #include "global.h" .... //file B.c #include "global.h" .... Build target 'Target 1' compiling A.c... compiling B.c... linking... *** ERROR L104: MULTIPLE PUBLIC DEFINITIONS SYMBOL: nGLOBAL MODULE: A.obj *** ERROR L104: MULTIPLE PUBLIC DEFINITIONS SYMBOL: nGLOBAL MODULE: B.obj oh,why the error do not happen when i write code "#include <reg51.h>" in several .c files. please help me,thank you very much.
"from THE C++ PROGRAMMING LANGUAGE,Bjarne Stroustrup" Note that C51 is not a C++ compiler. There are some subtle differences between the plain 'C'-like parts of C++ and true ANSI 'C'; therefore, for issues relating to an ANSI 'C' compiler (such as C51), you should stick to an ANSI 'C' book.
1,sfr & sbit are not ANSI C.But they're declarations,and they can be put in headers. 2,C51 is not a C++ compiler,it's true.In the book,there are namespaces and classes which can be put in headers.But I delete them.
"int a" tells the compiler to reserve space for an integer. If you put such a statement in a header file, and #include it in several places, you are telling the compiler to reserve space for several integers, each of which has the same name -- which upsets the linker. Hence, you use extern int a; which does not reserve any space, but merely makes a name public. The space will typically be reserved by another statement in some single .c file which defines an integer named "a". "sfr" and "sbit" do not reserve space. They merely identify bits that already exist in the registers of the processor, which cannot be created or destroyed by the linker. The statements are declarations rather than definitions, and thus can be safely included in headers. I agree it perhaps would be more consistent if it were "extern sfr" and "extern sbit". But then, no doubt some programmers would be left searching for the actual definition to go with the declaration.
"2, C51 is not a C++ compiler, it's true. In the [C++] book, there are namespaces and classes which can be put in headers. But I delete them." That's all good and fine for an experienced programmer who clearly understands these things - but recommending a C++ book to a novice who is obviously having some problems with basic ANSI 'C' concepts is, I suggest, somewhat unhelpful?
"int a" tells the compiler to reserve space for an integer. Close, yet not quite completely correct. The statements
int a = 0; int b = some_value;
int a;
extern int a;
int a; int a; int a = 5;
int a = 5; int a = 5;