ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a global.h for constants and it works great for #define and typdefs. With the arrays i get compile warnings or linking error.
I don't know any decent solution. I think there is no decent solution.
Sometimes I use a very ugly kludge to get the most important aspect of the solution, which is the ability to define the array once within an include file that is included into multiple .c files. But that means the syntax must be ugly for use of the array.
In the include file:
Code:
inline int const* a()
{
static int const a_static[] = { 13, 17, 39 };
return a_static;
}
In each .c file, where you would have used a[n], you must instead use a()[n]
Quote:
Originally Posted by dugan
You're not using include guards?
Include guards don't help this issue at all. The problem is not including the .h file multiple times in one compilation unit (which include guards would fix). The problem is including it into multiple compilation units, so it is multiply defined at link time.
Quote:
BTW, I'm quite sure that declaring variables in include files is not consistent with best practises.
Actually, declaring variables in include files generally is considered to be best practice.
We're talking about defining variables in include files. Good software architecture often calls for defining variables (especially const variables) in the include file where those variables are declared. But C and C++ provide no good way to define variables in include files (I think that is a flaw in both languages). So best practice in C is generally to go along with that language flaw by declaring variables in include files but defining them in .c files.
PTrenholme, were you answering that question? It appears not.
Quote:
Originally Posted by kalleanka
with constants i do
#define NAMEOFCONST value
Maybe PTrenholme was answering that non question. I also want to answer that non question.
#define in C and C++ is an ugly feature. Because it doesn't respect scope, its use quickly becomes a landmine in large projects. You should not use #define for ordinary integer constants because there is a better alternative.
As PTrenholme may be suggesting, enum is a better substitute (compared to #define) for the things you'd like to be able to do with const int but the language doesn't let you.
, but the former (SCOPE_PREFIX) should be mandated by project management. I.e. properly managed project first establishes naming convention - especially for global things. Class names are global too.
I apologize for answering the non question. You demonstrated one of the problems with answering non questions. I disagree with you about this whole scoping and project management issue. But I don't want to debate that in this thread (I expect it would be pointless to debate that anywhere).
My answer to the actual question of this thread is way back in post #6
johnsfine thats an intressting solution. And I agree that const declarations in includefiles are a flaw in c and c++.
enumerations would work for int but not for strings.
I think i shall encapsulate it in functions and call functions for the constants. Then its not a big step to use a settingfile for the constants if i need it in the future. I do want all the constants i one place. Its polltimes, messeges(diffrent languages), spacings, colors, layouts etc for a kiosk (wm, desktop, coinmech, servercommunication etc).
thanks to you all
I will sleep in it and use johnsfines solution or make functions in a .c file.
If you're willing to have the normal (for C and C++) split between a .h file and a .c file, then you could do all this the usual way, without the wrapper function.
In global.h
extern int A[];
In global.c
int A[] = { 0, 1, 2, 3};
In other .c files include global.h and use A[n]
I thought you had some reason to want the definition to be in the include file.
If you really prefer hiding the array reference in an access function, that is also OK. But do you care about performance? The method I gave to hide the reference can be mostly elided by the optimizer, so it has minimal impact on performance compared to the ordinary method (declare in .h, define .c and directly use in other .c). Your method cannot be elided by the optimizer, so it has a significant performance cost.
When intentionally hiding a data reference in a function, I generally put that function in an include file so it can be optimized out. For very best performance, you may need to put the actual data definition in a .c file (with an extern declaration in the .h). My trick of burying the data as a function static inside the access function might confuse the optimizer enough that it is only partially elided.
This is of course the best way. That i did not think about that. And performance might be a problem but i do not know for now. I have the hole system (100M) running in ram (1000M) and its for a kiosk with intel atom.
Thanks a lot. I really appreciate it.
ps I never took a course in c or c++ but i know java, pascal, lisp. C got more issues(memory, pointer, strings etc) for a programmer and i seem to fall into the trap all the time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.