Re: Precompiled header problem
Re: Precompiled header problem
- Subject: Re: Precompiled header problem
- From: Chris Espinosa <email@hidden>
- Date: Tue, 25 Apr 2006 10:17:58 -0700
On Apr 25, 2006, at 8:52 AM, Jeff Schmidt wrote:
A variant of this question has been previously asked by a coworker
on this list without a real solution. I'm going to try rephrasing it
and see if any Xcode/gcc experts here might have some additional
insight.
I have a large C++ project with a precompiled header. The entire
project, but not some sublibraries, specify C++ compilation for all
files (so only one PCH haunting me). This is using gcc 4.0 building
for i386, but it also happens building for PPC. On link, a selection
of constants in the precompiled header turn up undefined, and the
linker says they are referenced by the precompiled header. The
constants are in a namespace, contained in one header included as
part of the precompiled header, and other constants within that same
file are apparently OK. Another clue, it centers around two
constants who are then used in a series of other constant
definitions. They all end up undefined.
Header snippet:
namespace PortLib {
#if TARGET_MACOS || TARGET_WIN32
const LONG kMaxDocsOpen = LONG_MAX;
#else
#include <climits>
const LONG kMaxDocsOpen = LONG_MAX;
#endif
// Resolution of document space at 100% scale
const HIFLOAT kDocSpaceDPI = 72.0;
// Range for user scales
#if BIG_PAGE_MAX
const HIFLOAT kMinScale = 0.01;
#else
const HIFLOAT kMinScale = 0.25;
#endif
const HIFLOAT kMaxScale = 8.0;
const HIFLOAT kAlmostEqualDiff = 1.0 / (2.0 * kDocSpaceDPI *
kMaxScale);
... dozens of additional declarations ...
}
Many more undefined constants follow (derived from either
kDocSpaceDPI or kMaxScale or both).
Build log error snippet for code above:
/Library/Caches/com.apple.Xcode.501/SharedPrecompiledHeaders/
CreatorServerDebug-avefshvwsedytaauvaarzdqqgjnv/
CreatorServerDebug.pch.gch.o reference to undefined
PortLib::kDocSpaceDPI
/Library/Caches/com.apple.Xcode.501/SharedPrecompiledHeaders/
CreatorServerDebug-avefshvwsedytaauvaarzdqqgjnv/
CreatorServerDebug.pch.gch.o reference to undefined
PortLib::kAlmostEqualDiff
/Library/Caches/com.apple.Xcode.501/SharedPrecompiledHeaders/
CreatorServerDebug-avefshvwsedytaauvaarzdqqgjnv/
CreatorServerDebug.pch.gch.o reference to undefined PortLib::kMaxScale
... a dozen or so more undefined references in the PCH ...
There are plenty of other constants in this namespace that survive
compilation and linking just fine, what possible reasons would there
be for only these to fail? Could these actually be referenced
someplace else and causing the link error, contrary to the error
message? It's hard for me to fathom why constants defined in a PCH
would then turn out undefined -- referenced from that very PCH -- at
link.
Any expert tips or insights to help resolve this problem are welcome.
Well, there's a very simple explanation. const definitions are code,
and gcc stops precompiling headers when it reaches the first line of
code.
Precompiled headers are applied as a prefix to every source file in
your target. So if gcc emitted code for these const definitions in the
pch, you'd have multiple definitions of the constants, one for every
source file. The linker wouldn't like that.
You have two choices:
1) Use macros instead of const declarations.
2) Declare the consts as "extern" in your precompiled header, and do
the formal definitions in a single source file in your build (not in a
header file or the prefix file).
Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden