André Fratelli
André Fratelli

Reputation: 6068

Xcode/clang including the wrong file

I have a file called assert.h which defines several assertion macros. The project is called Core and lives in a folder with the same name. However, this file lives in Core/hul, which is a submodule of the project that implements some abstract utilities. Here's an excerpt of the file:

#if defined(HUL_DEBUG)
#   if defined(HUL_TEST)
#       define HUL_ASSERT(e)    HUL_TEST_ASSERT(e)
#   else
#       include <assert.h>
#       define HUL_ASSERT(e)    assert(e)
#   endif
#else
#   define HUL_ASSERT(e)        /* empty, do nothing */
#endif

As you can see, when HUL_TEST is defined assertion macros expand to a unit test assertion callback. That works fine. When compiling for release (e.g. HUL_DEBUG is not defined) it does nothing. Also fine. When compiling for debug (without testing), it includes the system's assert.h and defines a macro that expands to assert. Everything OK so far.

The problem is that regardless of including <hul/assert.h> or <assert.h> it's always hul/assert.h that is included, which is not what I want. This is one of the reasons that hul/assert.h is qualified under the hul folder.

The obvious first thing to check is Other C Flags and Header Search Paths. But the later is empty and the former is as follows:

-I../../include/Core
-I../../test/include/Core
-I../../test/include

As you can see, Core/hul is not included, so #include <assert.h> should not resolve to hul/assert.h. The question is, why does it? Am I missing some configuration?

Note: of course I could change the file's name, but I rather understand why this is happening. This framework will still grow immensely in number of files and I don't want to be worrying about this kind of conflicts.

Upvotes: 1

Views: 189

Answers (1)

Stephan Lechner
Stephan Lechner

Reputation: 35154

set USE_HEADERMAP = NO. When set to YES, XCode uses a dictionary that maps a header file name to a path where to find in order to speed up compilation. So regardless where you place your header file, if it has found his way into this map it will be found forth on.

Another way would be to use absolute paths for all user header files, e.g. #include "./assert.h" (which should give you an error if the file is not located directly in the project directory or any manually defines user header search path).

Hope it helps;

Upvotes: 2

Related Questions