Xcode - Use header imports with folder-based paths - objective-c

I'm using a static library and pointing Xcode to a folder with the headers in it. These headers are organized in a hierarchical folder structure:
headers:
- a.h
- b.h
- subheaders:
- c.h
- moreheaders:
- d.h
I also have some prewritten source code that uses this library, and it refers to the headers based on their locations: #import "subheaders/c.h".
However Xcode flattens the folder hierarchy, forcing me to use #import "c.h". There's a good deal of code, and I can't rewrite it very easily to stop using the foldered imports. Any way to make Xcode recognize the folder structure?
P.S. I'm including these headers using the "Library Search Paths" "Header Search Paths" build setting under my primary Target. The search is non-recursive, so I don't know how it even finds the nested headers...
Thanks for your help!

Xcode builds header maps by default and header maps are flat, except from modules/frameworks, where the map is ModuleName/Header.h. For details about how Xcode builds these maps, which settings exist and what the compiler does, if a file is not found in a map, please see this answer.
Setting an appropriate header search path (HEADER_SEARCH_PATHS or USER_HEADER_SEARCH_PATHS) will make path includes possible, regardless if header maps are used or not, yet the flattened import will still work as well in that case. If you don't want the flattened import to work, you need to disable header maps or ensure that the header files in question are not added to the maps, as shown in the other answer.

Unfortunately this was just a case of my not being observant. I had added the headers to the search path, but I had also earlier added them as source to the project (to see if I could get it to work that way). When I removed the source folder and kept the header search path I could reference files by their full paths. Thanks for the help.

after Hours of try and fail i found an option in XCode 9.4.1 in an XCode 9.3-compatible project the option to disable the usage of
Ues Header maps
This will let Xcode recognize the project structure.
i althought set every file and folders location to be project relative
I Hope there is someone with the same problem and has now an solution

Related

File not found on Xcode9.3

I'm currently working on a project (single view application) in Swift on Xcode using a precompiled sdk (Parrot SDK3: http://developer.parrot.com/docs/SDK3/#ios).
My issue is that I have a "file not found" error whereas the file exist:
The headers I try to import are written in Objective-C (but I want to import these headers in a ObjC file).
I took care to write correctly the path to the header files and to avoid cyclic import. After a lot of clean/build and a lot of methods attempted I don't know what to do.
Xcode tries to find header files (.h) with a help of "search paths" list.
You can have multiple search paths in this list, for example if you have #import <a.h> in the program, and you have paths "/asd" and "/qwe" in your search path list, it will try to find the header as "/asd/a.h" or "/qwe/a.h" (it tries both).
You can add custom paths in Xcode project build settings. There's a section "Search Paths" and there you have "Header Search Paths". If your library is inside your project, then it is useful to use $(SRCPATH) variable in the path, which expands to your project root folder like so:
Note that if you import as <somelib/somefile.h>, and your path in "Header Search Paths" has something that ends with "include" like "/some_path/include", then the header must be at "/some_path/include/somelib/somefile.h", which is sometimes not true, because often people place header files right inside "include/", not inside "include/somelib/".
One way to solve this would be to make a symlink that points to the library "include" folder, with your custom name, and then add a parent directory of that symlink to your header paths.
For example, if you create an empty directory "libs/include/", and you have put your library files at "libs/dist/libARDiscovery/.../include", then you can create a symlink "libs/include/libARDiscovery" that points to "lib/dist/libARDiscovery/.../include", and then add "$(SRCROOT)/libs/include" to the header search paths.

Relative Path for Headers in Modulemap

Currently i am trying to optimize my modulemap. The problem was that i could not add my framework to my project because i used a obj-c Framework in my swift framework. I added the Obc-c headers in my modulemap and made the module work and could successfull add my framework to my project. The problem is that I cant make it work with realative path to my headers. And using absoute path is only a temp solution and not co worker friendly.
Right not I have to set this kinda absolute path:
header "/Users/me/Documents/framework_project/Folder/OBJC_LIB.framework/Headers/someHeader.h"
I added this modulemap in my framework project as private modulemap.
Is there any option to my this work like this?
header "./Folder/OBJC_LIB.framework/Headers/someHeader.h"
Thanks in advance.
First of all you need to clarify the location of your modulemap file. Probably it will be in the DerivedData folder (to see the location of this folder press Xcode -> Preferences -> Locations in Xcode 10), path will be similar to /Users/me/Library/Developer/Xcode/DerivedData. In such case you will find the modulemap file inside this folder, full path will be something like /Users/me/Library/Developer/Xcode/DerivedData/framework_project_something/Build/Products/Debug-iphonesimulator/framework_project/framework_project.framework/Modules/module.modulemap. Once you will find this file you will be able to use such declaration inside your modulemap file:
header "../../../../../../../../../../../Documents/framework_project/Folder/OBJC_LIB.framework/Headers/someHeader.h"
I know that this is not exactly what you want but maybe it will help you to find a better answer on this question.
Having encountered the same problem, I finally found that the HEADER_SEARCH_PATH of the Swift framework must contain the path to the ObjC framework's umbrella header.

import "cocos2d.h" works in some files, but not all

Here is what I did:
In Xcode 4.3.1
File -> New -> Project -> Single view application
Dragged the file cocos2d-ios.xcodeproj into navigator.
In build phases settings : Added : libcocos2d.a (becomes highlighted in red), as a linked library (required).
Added OpenGles.framework, Quartzcore, and libz.dylib
Changed build settings - Set "Always Search User Paths" to YES
Added cocos2d source directory to "User Header Search Paths"
Now, it seems I can type: import "cocos2d.h" , in the app delegate and root view controller that Xcode created. But if I create a new file, and I add the line "import "cocos2d.h"" to the top, Xcode complains that the file is not found. But it seems to build fine. Also, in this new file code sense does not work.
What should I do? Why can I import only in the files that Xcode created? Is there some setting I need to change so that in the files I create, I can import cocos2d ?
EDIT: It seems to build and run fine. I can call methods in the cocos2d api. code sense just doesn't seem to see cocos2d.h in the new files I create.
EDIT - it seems that code sense suggests cocos2d.h when i type : import "
But it does not suggest classes / methods from the cocos2d api.
Turns out I had only added the "user header search paths" to my target, but not in the project settings.
Adding it fixed the problem. I guess the reason it compiled fine was because I had added it to the target, but it didn't work in the text editor since I hadn't added it to the project.
This happened to me as well, even though I did add it to both project and target.
However, what worked for me was to select the RECURSIVE checkbox under "User Header Search Prefixes" (which was set to "/lib/**")
I had same issue.
My project -> Build Settings
Look for the entry for Search Paths
Always Search User Paths - YES
User Header Search paths - "myProjectName/libs" //I put box2d folder here
Hope this help someone.

Include issue: header file not found in iOS project in Xcode 4

Consider my setup looks like this:
XCode 4 iphone/ipad project
ObjC .m file that includes a new header file, this header is reported as "file not found"
ObjC .m file is located in root folder of the project
new header file is also located in root folder of the project
new header file was created within Xcode 4 as "C header file" (!!)
I tried using #import and #include statements with both "" and <> lookup syntax.
The project already consists of a bunch of other objC headers/classes and they work nicely. I suspect that creating the file as a C-style header is the issue.
I did explicitly NOT try to add a USER_PATH or anything like that, since it's all happening in the root project folder.
Any ideas? I'm really surprised how complicated such simple things can get!
! Additional comment: (EDIT)
I now verified that I can create any type of header (menu "new file"->"C/C++"->"header") and it will never be found by objective-C code. Even a new clean project behaves the same! What kind of feature is this?
Weirdness.
I talked to a bunch of fellow Xcode sufferers and they confirmed I HAVE to set the extra path variable. Their explanation was a bit odd though: in objectiveC the lookup (import) simply scans the whole subfolder structure, whereas an 'include' does not. From my point of view this would only be a valid argument if the header file in question was located in a subfolder! But mine is in the exact same location as the '.m' file!

Box2D compile errors

I've added Box2D to a Cocos2D project I'm working on.
I've followed several guides as to how to do this (all of which seem to differ!).
However, none seem to work.
I'm getting these types of errors:
error: Box2D/Collision/b2BroadPhase.h: No such file or directory
I assumed I'd got the Header Search Paths wrong but have tried all sorts of variants with no luck.
Any suggestions?
The easiest way to use Box2d with your project is to follow these lines:
Copy the Box2d files into a subfolder of your project.
Import these files into your project via Xcode.
After in the "Project navigator", select your target and open the “Build Settings” tab.
Set the "Always Search User Paths" to YES.
Then search for the "User Header Search Path" and add this "${PROJECT_DIR}" (think to check the “recursive path“).
That's all!
You just have to be careful when you want to use Box2d. Think to change the extension of your files from .m to .mm to warn the compiler that the class must be compile as Objective-C++ instead of Objective-C.
I have found a good tutorial here (with Xcode 3.2, but the idea is here). I hope it'll help you.
Alternatively ... After a lot of trouble trying to include box2d in my project, I instead used box2d as a static library - takes a minute to setup, but it's much easier to maintain / add to multiple projects. Step by step guide here:
http://red-glasses.com/index.php/tutorials/box2d-for-ios-made-easy-make-it-a-static-library/