XCode 4 C++ header file with relative path to anot

2019-08-09 01:54发布

问题:

I'm using a library with an include structure where the .h files are all in a single directory. These .h files contains a single line, a #include directive which points to the 'real' header file in specific source folder locations. The #include path in these files is relative.

So, here's an example. The directory structure is:

/project
     /sources 
         <my .cpp files>
         <my .cpp files>
         ...
     /include
         /component
             foo1.h
             foo2.h
     /platformA/something/foo1.h
     /platformB/somethingelse/foo2.h

/include/component/foo1.h contains a single line of code: #include "../platformA/something/foo1.h"

/include/component/foo2.h contains the single line of code #include "../platformB/somethingelse/foo2.h"

In my sources, I simply have: #include "component/foo1.h"

The header search path for my project points to /include

Now, XCode 4 is able to find component/foo1.h in /include, but it's unable to follow the relative include path within those headers and find the 'real' foo1.h in the `/platformA/something' directory, and so on.

I suspect it's because the include paths in the top-level foo1.h file is relative to its location, but XCode might be treating it as relative to some other location (project root or something)? FWIW, Visual Studio has no problems with an identical configuration.

Any ideas on what I can do to remedy this?

回答1:

From your directory structure it seems that the directories platformA and platformB are placed outside the include folder. There are two possible solutions to this:

Solution A

Move these to include folder.

Solution B

Add project/platformA and project/platformB to the directories where include files should be looked for in project settings.



回答2:

Don't use relative paths. Seriously, it's implementation-defined behavior how they work, so different compilers/environments/platforms will behave differently, and in your case, Xcode is almost certainly invoking GCC or clang in some sort of "build" directory, which may or may not be a sibling to your sources directory.

It's not worth the headache.

Put platformA and platformB in include, or add another directory (say, platform-include) put them in there, and add that directory to your include path.