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 11-28-2011, 05:12 AM   #1
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Rep: Reputation: Disabled
Global constants


Hi All,

how can i declare #define constants, so that it can be accessed from anywhere in the project,without #including the filename from where it as defined

Example
Code:
/*****file1.h**********/

#define MY_CONST 2
Code:
/*******file1.cpp*******/
//here i dont want to #include"file1.h" but i want to use MY_CONST

#ifdefined MY_CONST
.
.//some code
#endif
in Visual Studio 10 we can include this constant names in compiler settings,hence it can be accessed from any part of the project,but the same how can be done with g++ compiler/programmatically

plz guide me

waiting for your reply.........

Last edited by shamjs; 11-28-2011 at 05:23 AM.
 
Old 11-28-2011, 05:16 AM   #2
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
There's no way to do that with the preprocessor.

You could use extern variables in your C/C++ implementation code, perhaps.

But you probably shouldn't; just put all of your shared definitions into a common header file and include it when you need it. That's the convention.
 
Old 11-28-2011, 06:38 AM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

Take a look at these gcc options (from `man gcc')
Quote:
-D name
Predefine name as a macro, with definition 1.

-D name=definition
The contents of definition are tokenized and processed as if they appeared during translation
phase three in a #define directive. In particular, the definition will be truncated by embedded
newline characters.

If you are invoking the preprocessor from a shell or shell-like program you may need to use the
shell's quoting syntax to protect characters such as spaces that have a meaning in the shell
syntax.

If you wish to define a function-like macro on the command line, write its argument list with
surrounding parentheses before the equals sign (if any). Parentheses are meaningful to most
shells, so you will need to quote the option. With sh and csh, -D'name(args...)=definition'
works.

-D and -U options are processed in the order they are given on the command line. All -imacros
file and -include file options are processed after all -D and -U options

-include file
Process file as if "#include "file"" appeared as the first line of the primary source file.
However, the first directory searched for file is the preprocessor's working directory instead of
the directory containing the main source file. If not found there, it is searched for in the
remainder of the "#include "..."" search chain as normal.

If multiple -include options are given, the files are included in the order they appear on the
command line.

-imacros file
Exactly like -include, except that any output produced by scanning file is thrown away. Macros it
defines remain defined. This allows you to acquire all the macros from a header without also
processing its declarations.

All files specified by -imacros are processed before all files specified by -include.
You probably should add `-imacros header.h' to gcc command lines in your makefile.

Hope this helps.

Last edited by firstfire; 11-28-2011 at 06:39 AM.
 
Old 11-28-2011, 06:40 AM   #4
shamjs
Member
 
Registered: Sep 2011
Posts: 93

Original Poster
Rep: Reputation: Disabled
thanx for the needful...............
 
Old 11-28-2011, 06:53 AM   #5
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 211Reputation: 211Reputation: 211
Oh, yes, -D sort of does that kind of thing. But if you're going to use that, you should still have a default.

Suppose you have some code that uses macro FOO, and you compile with -DFOO=value. In this case, you'd be fine.

But suppose you forget, and don't compile without -DFOO; the program won't compile. Sure, you'll get a fairly descriptive error message, but still.

So, I'd still suggest a header file -- in this case, something like:

Code:
#ifndef FOO /* If we didn't get -DFOO on command line */
#define FOO default_value
#endif
 
Old 11-28-2011, 08:07 AM   #6
SigTerm
Member
 
Registered: Dec 2009
Distribution: Slackware 12.2
Posts: 379

Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by shamjs View Post
Hi All,

how can i declare #define constants, so that it can be accessed from anywhere in the project,without #including the filename from where it as defined
#define is not a constant, it is preprocessor macro. Constant is entirely different thing.
You can specify preprocessor macros for entire project using corresponding compiler command-line switch. See compiler documentation.
 
Old 11-30-2011, 12:32 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jhwilliams View Post
Oh, yes, -D sort of does that kind of thing. But if you're going to use that, you should still have a default.

Suppose you have some code that uses macro FOO, and you compile with -DFOO=value. In this case, you'd be fine.

But suppose you forget, and don't compile without -DFOO; the program won't compile. Sure, you'll get a fairly descriptive error message, but still.

So, I'd still suggest a header file -- in this case, something like:

Code:
#ifndef FOO /* If we didn't get -DFOO on command line */
#define FOO default_value
#endif
It's a recipe for disaster. In some places it will that FOO is BAR, and in other places it will be that FOO is DEFAULT_BAR, and there will be no warnings and no indication whatsoever of FOO's value, and the program won't work as expected.

It will take "years" to debug that - I had a very unpleasant experience with such undefined things getting the default value )though not in "C", but it doesn't matter).

There must be an error message in case of undefined constants.
 
  


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
LXer: OpenClinica Global Conference to Bring Together Global Community for Open Sourc LXer Syndicated Linux News 0 01-08-2010 10:50 AM
C - Constants in printf marcojrfurtado Programming 2 11-26-2009 09:14 AM
C++ Maximum values and global constants CoderMan Programming 9 09-29-2009 04:10 AM
How to check the cpu utilization on all non global zones from Global Zone rajaniyer123 Solaris / OpenSolaris 3 10-09-2008 01:43 AM
CSS - Constants? dushkinup Programming 1 05-02-2004 01:08 PM

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

All times are GMT -5. The time now is 03:46 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