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.
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
-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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.