Xcode:How to import parent's header in a sub-project - objective-c

I've a project that has a subproject (an XPC worker). Here I need to import one header from the main(parent) project. How do I do this?
I tried by setting(Sub project's) Header Search Path, User Header Search Path with values like $(SRCROOT) & $(SRCROOT)/../Interface.h. Also tried by changing the settings Recursive and Non-Recursive.

The way to solve these issues is to look at the actual compiler line when building the target to see what folders are being specified in the -I options and work from there. You need to go to the Build Log, find a file being compiled and then expand the command using the dropdown button thing on the right of the line.
The structure for all projects is fairly unique, so it's almost impossible to provide a one-size-fits-all fix for this.

Related

Xcode cannot find ProductModuleName-Swift.h

I'm attempting to import my "-Swift.h" file into one of my Objective-C .h files but xcode keeps telling me that the file doesn't exist
#import "Aesculus-Swift.h"
If I command click on the file name it will take me to the generated header file so I know it exists. Why is xcode not able to find it?
This seems like just another issue with Xcode and it's complex tool chain of static analysers and compilers.
Openradar lists radar://21362856 - Swift to Objective-C bridging is unreliable. I am sure there are more but I stopped looking after finding one for this example.
The author imarcelv notes in the description:
I asked a Swift engineer at WWDC in a lab and even he didn't know how to fix this issue.
Steps to Reproduce:
Add a ramdom Swift class to an Objective-C project
Add the #import "ModuleName-Swift.h" file that Xcode generates automatically
Try to use it or just try to compile the project
From time to time it simply doesn't work
It's probably best to file a radar on this issue as it seems that others are already calling it out.
One other thing you could try...
Historically, it was possible for Xcode to completely lose it's syntax highlighting and you could always find out what files the static analyser was giving up on by increasing log level of clang.
I'm not sure if it's still relevant but if I was in your position I'd be trying this command:
defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3
This generates logs you can search with using Console.app for just xcode to highlight the messages. You'll want to trash the derived data of your project to force it to re-compile things.
Although not the same issue as what you're seeing, I have had this post on the syntax highlighting issue bookmarked for years for the above defaults write command to try in times like these.
I solved this recently by adding the following entry to my .xcconfig (you could add it in Xcode's Build Settings > User Header Search Paths if you prefer).
USER_HEADER_SEARCH_PATHS = $(BUILT_PRODUCTS_DIR)/MyFramework.framework/Headers
This tells the compiler to search for headers in the build output directory, which is where Xcode puts the generated header (at least in the case of this framework).
In my case this is a directory like ~/Library/Developer/Xcode/DerivedData/MyProject-LongCode/Build/Products/Debug-iphonesimulator/MyFramework.framework/Headers/MyFramework. You might find your generated header in there too.
Xcode's header and dependency management is a hot mess, and it's not surprising that it doesn't work for you.
I had trouble with this stuff & found that your -Swift file is the Product name of your Target ( not just the name of your Target ) . I found the details here helpful: http://ericasadun.com/2014/08/21/swift-calling-swift-functions-from-objective-c/
When you encounter such situation, just find your kinda "ProductName-Swift.h" file by just cmnd+click on it (even if xcode shows warning about it is not found, the #import "Aesculus-Swift.h" string is still clickable) and then in opened code editor window choose context menu and "Show in Finder" item, then explicitly add it to your project.

Xcode - Use header imports with folder-based paths

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

XCode warning - duplicate CMakeLists.txt while trying to change project name

I tried to change the name of my xcode project, (which uses the cocos2d framework), but for some reason my main files folder (the one that contains all the .h and .mm files) wouldn't change the name. so then i manually clicked under "MyProj TARGETS -> Build Settings" and manually changed the fields:
Info.plist File
Header Search Paths
User Header Search Paths
Prefix Header
to be using "MyProj/" instead of "PrevName/" (the previous name i had named the project) but now i'm getting these two strange warnings:
Warning: Multiple build commands for output file /Users/me/Library/Developer/Xcode/DerivedData/MyProj-fzlkaghtvjzkgkenbpejhzwvxsft/Build/Products/Debug-iphonesimulator/MyProj.app/CMakeLists.txt
Warning: The Copy Bundle Resources build phase contains this target's Info.plist file 'MyProj/Resources/Info.plist'.
you'll see that this is also mentioned under: http://www.cocos2d-iphone.org/forum/topic/33245 but no one has answered this part
and i tried to look under Xcode warning: "Multiple build commands for output file" to see under "Build Phases -> Copy Bundle Resource"... and indeed, there were 2 CMakeLists.txt files. one is:
MyProj/libs/Box2D
and the other is:
MyProj/libs/kazmath/src
which one do i need? or do i need both, since i think xcode used to be compiling with both??
i should mention that my project seems to be running okay right now if i leave the warnings in. but i don't like warnings that i don't understand, and i know that i could just swap my source/assets to a new project to get rid of the warning, but i'd like to know for future reference how to properly rename everything in a xcode project.
Thanks
Oh wow... I had no idea the traffic on stackoverflow is so low these days...(or at least for xcode & iOS dev) sighs
Anyways, I think I figured out a fix.
I read apple developer guilde for the second Warning
For the first Warning, I looked at another file that I had created, and I compared my project's settings to the other project's settings. (such a simple solution, duh, why didn't I think of this >_<) and it turns out, under Copy Bundle Resources, you're only supposed to have:
Your .png files and other resources used in code (but NOT your plist file, for the very reason given in the link above)
iTunesArtwork
ChangeLog
Actually, i'm not sure if you even need #2 or #3. I'll give an upvote to whoever experiments with it. So.... the solution is to simply remove all those other CMakeLists and cmakes and all that junk, including plist :)

Import Box2D : file not found

I'm trying to import Box2D library but I have a compilation error : 'Box2D/Box2D.h' file not found.
I tried lots of things to solve this problem but without any success. I'm not using cocos2d so I don't have any templates.
I just drag and drop Box2D folder in my Xcode project (tried to check and uncheck the 'copy to destination folder).
Tried to check 'Always Search User Path' etc ... No success.
If someone has the solution : god bless you !
Just drag and drop the Box2D.h/Bow2D.h to your project.If it is not as simple as that, I can recommend you this tutorial which explain everything you need to know to set up Box 2D in XCode.
EDIT
I have just tried the project in the tut I referenced and i believe you didn't set-up your header search path. Box2D files are intended to be included like this: #include so you need to go to Build Settings then add an entry for the Debug and Release key of the Header Search Paths (under Build Settings)
you are looking as something like this: Debug Box2D_v2.1.2/Box2D

Removing a method call from inside a static lib(.a) without recompiling

I'm using a static lib thats giving me a warning when uploading my binary for review by apple.
The method in the static lib that causes the warning(non-public selectors) is never called by me, its corresponding .h is deleted from my proj, but warning still persists.
Given that I know the method name causing the problem, is there a way for me to open/edit this .a and comment/delete the offending piece of code and then use the modified .a in my project.
I don't have access to the .a source to recompile it, and its very old and the creator of it has no contact details for me to track down.
Many Thanks,
-Cake
Quick and dirty solution: Open the .a file in a hex editor and change all instances of the name. Leave the function name the same length so that offsets in the file don't change, just change a letter or something like that. I did a quick test, adding a dummy function to a subproject we're building as a static library then tweaking the function name in the .a file (there were five instances, for what that's worth) and everything built okay. I don't see any reason it wouldn't pass the App Store check after that.
I'm really surprised the function was still there in the final build, though—I thought Dead Code Stripping was supposed to clean out any unused code. Huh.
http://opensource.apple.com/source/cctools/cctools-809/
I don't presume to get your bounty, because I haven't provided an easy solution. But yes, it in theory is possible. You have your work cut out for you.
There are several solutions, depending on your lib and project.
In your build settings :
Enable "dead code stripping" if possible : If the method is never used (even internally), the symbol will be deleted.
Use "Unexported symbol file" : Simply add the symbol into a file and it will be removed from the binary. This will work even if the symbol is used internally.
Enable "Deployment Postprocessing" and "Strip Linked Product" with "Strip Style" set to "All symbol"
(Not sure) Use "Symbols Hidden by Default". This is related to the code generation and should not affect linking, but just in case everything above failed...
No need to hack the binary files. Just turn off the compiler's "unused selectors" warning: -fno-unused-selectors.