LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2006, 11:51 PM   #1
kranti
LQ Newbie
 
Registered: Oct 2005
Location: Pune
Posts: 17

Rep: Reputation: 0
How to use conditional #define?


Hi,

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.

How should I implement this?

Thanks,
Regards,
Kranti
 
Old 09-20-2006, 01:40 AM   #2
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 09-20-2006, 04:28 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
as wim said, you can't use pre-compiler directives for this.

you could use an environment variable
probly the easiest way.

man getenv

Last edited by bigearsbilly; 09-20-2006 at 04:30 AM.
 
Old 09-20-2006, 09:06 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
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.

Hope, this is useful.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Conditional Compilation oulevon Programming 3 02-26-2006 08:21 PM
CASE conditional la_mafia63 Linux - General 3 07-24-2005 01:02 PM
c compound conditional steve_96 Programming 9 06-04-2005 07:17 PM
sed substitution conditional frostillicus Linux - Newbie 3 04-17-2005 12:36 AM
OS Conditional Defines kevin.cowley Programming 1 09-24-2003 03:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration