header file define in make file

Feb 12, 2007 3 Replies

Hi all,



I am using cosmic compiler for software development. Now, I am just curious whether can I do the following...



In make file Define label GLOBAL_PATH = $(BASE)\control\global.h



Then used GLOBAL_PATH everywhere in header files



#include GLOBAL_PATH instead of #include "..\control\global.h"



Let me know you two cents. Thanks.



Variables defined in makefiles aren't generally exported to the compiler, but they have to be explicitly provided as command line options added to the compiler flags. On most compiler this is done with a '-D' flag. Consult your compiler documentation to be sure.

In addition, you must provide escaped quotes around the filename.

So define:

GLOBAL_PATH = \"$(BASE)\control\global.h\"

and call your compiler with -D option

$(CC) -DGLOBAL_PATH=$(GLOBAL_PATH)

Or add it to the CFLAGS variable.

It's been a while since I used Cosmic. But the short answer is (I believe) "Yes."

You will have to add an option to the CFLAGS in your makefile. Something like -DGLOBAL_PATH=$(GLOBAL_PATH).

HTH,

-=Dave

Not as written, but it's possible to do something very much like it. See the other replies for details.

BUT:

neither of these really is a good idea. Don't put path names in #include statements. Not from the outside (like GLOBAL_PATH), nor from inside the source file itself. Use your compiler's method of specifying include file search paths from outside the source, instead. It's usually the -I option.

And if you choose to ignore this advice and put path names into your C source, you _definitely_ don't want to spell them with single backslashes. Use forward slashes (they work on DOS/Windows, too!), or double them up.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required