Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / CodeWarrior / December 2004



Tip: Looking for answers? Try searching our database.

Constants thru multiple files in C

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gabriel Nivasch - 13 Dec 2004 20:45 GMT
Hi, this is a question about C.

Is there a practical way to declare a constant in one file using
"const" and access it from another file?

For example, I can put in file 1 the global declaration:

int myconstant = 5;

and in file 2 I can do:

extern const int myconstant;

But then "myconstant" is a constant only in file 2.

If instead I write in file 1:

const int myconstant = 5;

then the compiler gives a link error.
How does this make sense?

Thanks,
Gabriel
MW Ron - 13 Dec 2004 23:44 GMT
>Hi, this is a question about C.
>
>Is there a practical way to declare a constant in one file using
>"const" and access it from another file?

You need to put the const in a common header file.

>For example, I can put in file 1 the global declaration:
>
[quoted text clipped - 15 lines]
>Thanks,
>Gabriel

Ron

Signature

Metrowerks Community Forum is a free online resource for developers
to discuss CodeWarrior topics with other users and Metrowerks' staff
       --   http://www.metrowerks.com/community  --

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com

Gabriel Nivasch - 15 Dec 2004 00:15 GMT
>You need to put the const in a common header

That gives me a different link error: "multiply-defined myconstant".

I made a file "myconstant.h" with the line

const int myconstant = 5;

I included myconstant.h in file 1 and file 2. Then CW gives a
"multiply-defined" link error.

Gabriel
MW Ron - 15 Dec 2004 02:43 GMT
>>You need to put the const in a common header
>
[quoted text clipped - 6 lines]
>I included myconstant.h in file 1 and file 2. Then CW gives a
>"multiply-defined" link error.

did you take the declaration of myconstint  out of your source file?

I bet that you didn't.

Ron

Signature

Metrowerks Community Forum is a free online resource for developers
to discuss CodeWarrior topics with other users and Metrowerks' staff
       --   http://www.metrowerks.com/community  --

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com

Arthur Langereis - 15 Dec 2004 09:36 GMT
> did you take the declaration of myconstint  out of your source file?
>
> I bet that you didn't.
>
> Ron

And of course you added a

#pragma once

at the top of the header file? Or, alternatively, enclosed the header
contents in something like (naming of header defines is arbitrary, I
just use this format):

#ifndef __MY_HEADER_H__
#define __MY_HEADER_H__

/* your header stuff here */

#endif // __MY_HEADER_H__

BTW, this is all pretty basic C stuff, you might want to read a decent C
primer, there are tons of books and sites covering C / C++.

Regards, Arthur
MW Ron - 16 Dec 2004 20:30 GMT
In article <41c0059b$0$36860$e4fe514c@news.xs4all.nl>,
Arthur Langereis <arthurREPLACEWITHUNDERSCOREpublic@xfinitegames.com>
wrote:

>> did you take the declaration of myconstint  out of your source file?
>>
[quoted text clipped - 9 lines]
>contents in something like (naming of header defines is arbitrary, I
>just use this format):

#pragma once may not be portable so below is better... but....

>#ifndef __MY_HEADER_H__
>#define __MY_HEADER_H__
>
>/* your header stuff here */
>
>#endif // __MY_HEADER_H__

This  is a very common programmer error.

Leading underscores and leading underscore and an uppercase letters are
reserved for library  vendors.   The end programmers should just use
regular symbols so that there never is a problem with a library vendor.

The mistake comes in because people look at headers like MSL and see
that style being used and adapt it.  

Ron

Signature

Metrowerks Community Forum is a free online resource for developers
to discuss CodeWarrior topics with other users and Metrowerks' staff
       --   http://www.metrowerks.com/community  --

Ron Liechty - MWRon@metrowerks.com - http://www.metrowerks.com

Arthur Langereis - 23 Dec 2004 10:04 GMT
> #pragma once may not be portable so below is better... but....

Agreed, but CW, VC and GCC support it, so for my simple closed source,
Mac OS X (and perhaps later Win32) application needs it will do fine.

I saw this in a source file I found online:

#ifndef ACE_ARG_SHIFTER_H
#define ACE_ARG_SHIFTER_H

#if !defined (ACE_LACKS_PRAGMA_ONCE)
# pragma once
#endif /* ACE_LACKS_PRAGMA_ONCE */

/* ... all header stuff ... */

#endif /* ACE_ARG_SHIFTER_H */

So, they wrap the entire header in an #ifndef XXX AND then, if they have
statically determined that #pragma once is available in the compiler,
they add that too! Am I missing something or is this just really
redundant? I can understand the use of a #define to directly test for an
included header, like Apple does in most of it's headers, but doing both
seems a bit much? MSVC also does this when it generates a header and
I've seen this construct quite frequently elsewhere, but I cannot see
the use of this. Can somebody explain?

>>#define __MY_HEADER_H__
>
[quoted text clipped - 6 lines]
> The mistake comes in because people look at headers like MSL and see
> that style being used and adapt it.  

Ah, thanks for the heads up. I never ran into problems with this since I
don't really use external libraries, but I'll modify it if I still find
one in my sources (I always just use #pragma once now.)

  Arthur
David Phillip Oster - 23 Dec 2004 17:55 GMT
In article <41ca9816$0$144$e4fe514c@news.xs4all.nl>,
Arthur Langereis <arthurREPLACEWITHUNDERSCOREpublic@xfinitegames.com>
wrote:

> > #pragma once may not be portable so below is better... but....
>
[quoted text clipped - 22 lines]
> I've seen this construct quite frequently elsewhere, but I cannot see
> the use of this. Can somebody explain?

If all you've got is #ifndef guards, then the compiler has do file i/o
to fetch the header file, and it has to scan the newly-read-in .h file
looking for the closing #endif. With #pragma once, or #import, all that
work can be skipped for a redundant include.

Signature

David Phillip Oster

Miro Jurisic - 23 Dec 2004 19:23 GMT
> If all you've got is #ifndef guards, then the compiler has do file i/o
> to fetch the header file, and it has to scan the newly-read-in .h file
> looking for the closing #endif. With #pragma once, or #import, all that
> work can be skipped for a redundant include.

Actually, all that information can be cached and this is, AFAIK, a common
optimization in modern compilers. (This also eliminates benefits of using the
#ifndef/#include/#endif idiom.)

meeroh

Signature

If this message helped you, consider buying an item
from my wish list: <http://web.meeroh.org/wishlist>

Eric Albert - 23 Dec 2004 17:57 GMT
In article <41ca9816$0$144$e4fe514c@news.xs4all.nl>,
Arthur Langereis <arthurREPLACEWITHUNDERSCOREpublic@xfinitegames.com>
wrote:

> > #pragma once may not be portable so below is better... but....
>
[quoted text clipped - 18 lines]
> they add that too! Am I missing something or is this just really
> redundant?

Yeah, it's completely redundant.  It'd only be required if a compiler
didn't implement #ifndef/#define/#endif correctly and did implement
#pragma once, but any compiler that did so wouldn't be compliant with
one of the most simple aspects of ANSI C (to say nothing of subsequent C
standards) and nobody would use it.

The only other reason to do this is that a compiler might optimize the
#pragma once case when including header files to know that it should
check for duplication of the header file now and avoid preprocessing the
rest of the file a second time.  But any good compiler should do the
same optimization for a header file completely surrounded by
#ifndef/#define/#endif anyway, and the need for that optimization is
largely eliminated by precompiled headers anyway (which are now
supported by all of CodeWarrior, Visual C++ and GCC).

-Eric

Signature

Eric Albert         ejalbert@cs.stanford.edu
http://outofcheese.org/

larry@skytag.com - 16 Dec 2004 17:58 GMT
> >You need to put the const in a common header
>
[quoted text clipped - 6 lines]
> I included myconstant.h in file 1 and file 2. Then CW gives a
> "multiply-defined" link error.

Try this in "myconstant.h":

extern const int myconstant;

And in "myconstant.c":

const int myconstant = 5;
Then #include "myconstant.h" wherever you need it.

Larry
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.