Prev: Reference to an undefined assignment operator of a base class in a derived class.
Next: How to convert a Borland C++ App to Visual Studio App
From: Robby on 22 Dec 2009 12:02 "Ron Francis" wrote: > I tend to do the same as Leslie. > Probably not applicable in your case if you only have one global variable, but what I have done is > create a header file with a list of externals that gets included in any files that need them > =====================external.h > extern long MQ[10]; > //add any externals that you need > > In this way, if you need to create any more variables that you need to access from other files, you > can just add it to the one header rather than to each file. > It may be less obvious, but it can save some legwork if there are lots of files and you need to > change something. So in this case, I would still have to populate the MQ array in another .c file? right? Regards Rob
From: Robby on 22 Dec 2009 12:44 Hello Ron, one more thing, I have tried your suggestion but it compiles with an error. Here is the code: ===============KERNEL.h enum enumKM{KM_QUIT = 0, KM_CREATE = 1, KM_RECUR = 2}; extern long MQ[10]; ===============KERNEL.c #include <stdio.h> #include "KERNEL.h" #include "API.h" int main() { API_InsertMessage(KM_QUIT); return 0; } ===============API.h void API_InsertMessage(enum enumKM m); ===============API.c #include "API.h" #include "KERNEL.h" void API_InsertMessage(enum enumKM m) { long h=1; MQ[1]= h; } ========================== The following error is reported at compile time: 1>API.obj : error LNK2001: unresolved external symbol _MQ And I did include KERNEL.h in API.c as you indicated ????? >Probably not applicable in your case if you only have one global variable, but what >I have done is create a header file with a list of externals that gets included in any > files that need them -- Best regards Roberto "Ron Francis" wrote: > "Robby" <Robby(a)discussions.microsoft.com> wrote in message > news:D7E4F275-613E-46DF-9736-09DFB6DB4BD2(a)microsoft.com... > > "Leslie Milburn" wrote: > > > >> My preferred approach is to declare the variable at the top of the .c module > >> in which it is first created and usually (but not always) populated. Then > >> for each .c module that requires access to the variable I place "extern > >> variable name" at the top of that module with a comment indicating why I am > >> using it. To make the code more readable I use the naming convention of > >> modulename_variablename to help identify where the declaration is located. > >> So in your case it would be api_MQ. > > > > So leslie, please confirm to me that what you are suggesting is this: > > =====================KERNAL.h > > //... > > > > =====================KERNAL.c > > #include "KERNAL.h" > > long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; // <at top of a particular .c file! > > > > =====================API.c > > #include "API.h" // API.h not showm !!! > > extern long MQ[10]; // Global Message queue variable > > > > void API_InsertMessage(enum enumKM m) > > { MQ[0] = m; } > > =========================== > > > > I have not tried the above code, but from the paragraph you wrote it seems > > to be what you are recommending! and its okay with me. > > > > However, my original sample would work aswell... to be noted here, MQ is my > > only global variable I need in my whole project. The rest of the application > > declares structures and preprocessor commands in header files. > > > > Thankyou all for your replies. Sorry for the delay! > > > > regards > > Robert > > I tend to do the same as Leslie. > Probably not applicable in your case if you only have one global variable, but what I have done is > create a header file with a list of externals that gets included in any files that need them > =====================external.h > extern long MQ[10]; > //add any externals that you need > > In this way, if you need to create any more variables that you need to access from other files, you > can just add it to the one header rather than to each file. > It may be less obvious, but it can save some legwork if there are lots of files and you need to > change something. > > Ron. > > . >
From: Robby on 22 Dec 2009 14:59 Ron, I don't think I would create a header file just dedicated to global externs. Becuase, then why not dedicate a header file for more than just extersn... why not just for externs and #defines and where do you draw the line as to what else we can put in there. So I would be mre comfortable with a KERNEL.h and put the exters, #defines and some struct declarations and include KERNEL.h in every .c file. So getting back to your suggestion, I guess that you meant it to be coded this way: ===============KERNEL.h enum enumKM{KM_QUIT = 0, KM_CREATE = 1, KM_RECUR = 2}; extern long MQ[10]; ===============KERNEL.c #include <stdio.h> #include "KERNEL.h" #include "API.h" long MQ[2] = {1,2]; int main() { API_InsertMessage(KM_QUIT); return 0; } ===============API.h void API_InsertMessage(enum enumKM m); ===============API.c #include "API.h" #include "KERNEL.h" void API_InsertMessage(enum enumKM m) { long h=1; MQ[1]= h; } ========================== Now the above compiles without errors or warnings. Thanks all for your replies. Very appreciated
From: Leslie Milburn on 22 Dec 2009 19:37 "Robby" <Robby(a)discussions.microsoft.com> wrote in message news:1955771B-260E-4B26-8447-F20572132C52(a)microsoft.com... > > In all due respect Leslie, I don't see where I am placing code in header > files. I consider this to be code: long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; So is THIS extern long MQ; IMO they should not be placed into a header file even for a shortcut. Your original approach to which I was referring to does have these declarations in headers files hence you have code in header files. So yes your original approach does work but I do not like it. Remember just because it can be done doesn't mean it should be done. Just take a look at the windows header files to see Microsoft created spaghetti. Leslie.
From: Igor Tandetnik on 22 Dec 2009 19:47
Leslie Milburn <CDB4W(a)NOSPAM.bigpond.com> wrote: > "Robby" <Robby(a)discussions.microsoft.com> wrote in message > news:1955771B-260E-4B26-8447-F20572132C52(a)microsoft.com... >> >> In all due respect Leslie, I don't see where I am placing code in >> header files. > > I consider this to be code: > > long MQ[10] = {1,2,3,4,5,6,7,8,9,0}; > > So is THIS > > extern long MQ; > > IMO they should not be placed into a header file even for a shortcut. How do you propose to declare a global variable then, so that it can be shared between source files? > Your original approach to which I was referring to does have these > declarations in headers files hence you have code in header files. If you can't have declarations in headers, what _can_ you have in headers? -- With best wishes, Igor Tandetnik With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925 |