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 flag variable in a file, which will take only 2 values, either 1 or 0. In my main application I read this file to get value of the flag. Now depending upon the value of flag I need to declare variables and call functions which are across many C files.
If I use #define in any function (after reading the flag) it does not get a global scope hence cannot be used with #ifdef everywhere. Even setting a global variable and using it with if-else does not allow me to decalre variables which will have a scope through out the function.
There's a limitation that I cannot use flags in Makefile.
Let me try to understand. Your main app reads a file at runtime (so after compile). Next you want to use the value to declare variables.
If that's the case: you can't do that. The reading is done at runtime (when your app is running) and the declaring of variables is done at compile time. Also #define is a precompiler statement so will be used when you 'compile' and not when you run the application.
Hello, kranti.
If your flag must affect the behaviour of your big (many files) program and you get the value of flag in runtime, then you can define the flag in each module as
Code:
extern int flag;
and in main function as
Code:
int flag;
So, after compiling if you change the value of `flag' anywhere, then each module will know about this. If you have more than one flag, it may be useful to put all flags in one header file and include it in each module.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.