Per file compiler flags in Xcode 8.1 - xcode

I need to add some specific compiler flags only for certain files in my project. I did some googling and found out that this used to be possible in the file inspector in older Xcode versions. I'm on Xcode 8.1, however, and I can't seem to find where I can add custom compiler flags for certain files.
Could anybody please tell me how this is possible with Xcode 8.1?

Go to the navigation bar of your project
Click on your project
Click on the target you want to modify
Click on Build Phases
Look for the file you want to add the compiler flag in the Compile Sources list; in this list you should see two columns: name and compiler flags
Click on the latter column and a popup window should come up; add the compiler flags to that specific file

Related

XCode 4 - Release Mode Flags, Build Flags, Link Flags

I'm working with XCode 4.6 on MAC OS X 10.8.
I'm new to this IDE and platform.
I have a project I compile and optimizations are turned off by XCode.
It doesn't matter if I set Release Scheme.
I saw at the thread named "Cannot Disable Debugger with XCode 4.5" that it has to do with the flags '-O0' and '-Os'.
I know that some flag set "Debugging Mode" which means no optimization is done and more data is generated by the code to help debugging.
I just can't find the single parameter which controls this behavior in XCode (Like /Mtd in VS).
Which parameter in the XCode options controls this parameter?
Moreover, In VS it is easy to see the build parameters as a command line (Under options, both Linker, and Build).
Where can I see that in XCode?
Thanks.
All the compiler and linker flags can be found within a project's or a target's "Build Settings" (targets can overwrite their projects' settings). To get there, select the project file, then select your current target, and go to "Build Settings". Select "All" and "Levels" at the top to display all available options and where they are set (project or target -- left-most wins).
The options are grouped, e.g., linker options and compiler options, and most of them refer to flags. Xcode shows a descriptive name rather than the full flag name. To see the latter, select the line of interest and select the "Quick Help Inspector" in the right panel ("Utilities" panel). The flag you seem to look for is called "Optimization Level" in Xcode.
You can set each option's value, and Xcode may display some meta information next to the raw value, e.g., "Fastest, Smallest" for -Os, or "None" for -O0. You can set all options for each configuration (by default, Release and Debug) independently. Which configuration is actually used depends on your build action. If you build & run, Debug is used by default. If you archive an application, Release is used by default. However, you can change most of that by editing the schemes.
Update:
To get an idea of the differences between the Release and Debug configuration, look at the "Build Settings" of a given target. If an option's value differs in both configurations (or any other configuration you may have), there's an arrow in front of the option's name which indicates the differing values. E.g., take the "Build Active Architecures Only" setting. In debug mode, it assumes you build for a particular testing device so there's no need to include code generated for other platforms as well. However, if you build your app for distribution, you want to build one app that includes code for all target devices/hardware architectures.
Other options to look into:
-Debug Information Format: The way debug information (crash reports) are stored. In Release mode, this is set to "DWARF with dSym", which means that your crash reports are encrypted so no one else can get meaningful information (symbol names) out of them.
-Strip Debug Symbols During Copy
-Optimization Level, as mentioned above
-Preprocessor Macros: This allows you to make your code (statically) dependent on the build configuration, e.g., to log only in Debug mode
Xcode provides more (and probably more accurate) information on all these options in the above-mentioned "Quick Help Inspector", just select any option.
From my experience, despite the optimisation, Release builds are often faster (i.e., they build faster) and definitely smaller in size.

How can I enable Xcode refactor options for an external build project?

I want to use XCode as an IDE for developing some AVR code. I did the following
1) create new project
2) select external build system
3) indicated i want it to be make, and told it NOT to "Pass build settings in environment"
4) copied my .c/.h/Makefile into the same directory i created the project in
5) added the .c/.h files to the project/target
6) verified that i can build (make) from both command line and Xcode
But I cannot use any of the refactor menu options, or jump to/reveal options. There appears to be no symbol information. Is there no way to use Xcode as more than just a simple C editor that uses an external build system????
If not, it's a bust for embedded development. Please tell me I'm wrong :(
I found a workaround/solution. Create a second target called "Dummy" or something like that, that is based on the Command Line Tool template of type C. Select all the .c files and in the "Targets" pane on the left side bar, check the new Dummy project (or if the options change, as they seem to often, do whatever gestures necessary to make Dummy depend on your C files).
It will create a Dummy directory and a couple of other Dummy files which you can remove if you want. You never bother building 'Dummy', in fact, if it's an embedded project with separate toolchain, likelihood is nearly 100% that it won't compile as a Command Line Tool for OSX anyway. :)
Apparently XCode needs this indirect hint to know that your .c files are indeed real C files and can be parsed/introspected as such.
As you add new .c files which your external build system likely see automatically, make sure you include them as a dependent for the Dummy target.
I was able to get this to work using XCode 6.1. In my case I'm using C++ but it should also work with C as some of my C++ files make C function calls. Here's what I did:
Create a dummy target using: File->New->Target
For the template choose: OS X->Application->Command Line Tool
Click Next
Fill out the Product Name, etc.
Select C++ for the Language field.
Click Finish.
Back in the Project Navigator pane:
Right click on the new 'dummy' target
Select: Add files to 'your project name'
Browse to select your directories/files to add.
Once the files were added to the Dummy target, I could then open a file under my normal target, right click on a method and Refactor, Jump To Definition, etc.

Can you batch change per-file compiler flags in Xcode 4?

I'd like to be able to change compiler flags for a whole set of files.
When accessing per-file compiler flags from the "Build Phases" area you can change flags one at a time, however say I'd like to change the flags for every file in a group (e.g., the DropBox or Facebook SDK).
Anyone know how to do this in Xcode 4.0.x?
Fortunately, YES :-)
In Targets->Build Phaes->Compile Sources, mark all the files you like to set a compiler flag for, press RETURN (hit the enter key!), put the flag in the upcoming window and you're done!

Three20 v1.1 and Xcode 4

Is there an easy way of getting Three20 v1.1 to work under xCode 4?
I followed the steps on http://three20.info/article/2011-03-10-Xcode4-Support but that seems to be for the master. I use v1.1 as I use the TTLauncher.
The project builds fine with a normal build, and runs perfect on an iPhone.
But when I want to create the archive (for the IPA for distribution) then I get 18 errors and 8 warnings.
The first one is:
../scripts/Protect.command: line 23: cd: /Users/XXX/Library/Developer/Xcode/DerivedData/iDoms-cmyivarwxpbvqdfnyclqdrikrtmw/ArchiveIntermediates/iDoms/BuildProductsPath/Release-iphoneos/../three20/Three20UICommon: No such file or directory
Most others are 'no such file or directory' on e.g.:
#import "Three20Core/TTGlobalCoreLocale.h"
or 'undeclared (first use of function)' which comes from the 'no such file or directory issue I presume'.
In my 'Header search paths' I have:
$(BUILT_PRODUCTS_DIR)/../three20
$(BUILT_PRODUCTS_DIR)/../../three20
../../three20/Build/Products/three20
/usr/include/libxml2
Any help would be very appreciated!
Just to add to Colin's answer: you can use Xcode's dependent projects feature to use three20, even though the three20 website says it's not "officially supported":
In your workspace, right-click on the empty space in your project navigator. Select "Add files to "My Workspace", and select the three20 project (three20/src/Three20/Three20.xcodeproj). This is the only project you need to add: it is not necessary to manually add the three20 subprojects.
Select your project in the navigator, go to Build Phases, open "Link binary with libraries", click on the "+" in the bottom left corner, and add the Three20 libraries: libThree20.a, libThree20Core.a, libThree20Network.a, libThree20Style.a, libThree20UI.a, libThree20UICommon.a and libThree20Navigator.a. This will cause Xcode to build Three20 when you build your project.
Add ${BUILD_DIR}/three20 to your include path as per Colin's answer: go to build settings, double click on "Header Search Paths", and add ${BUILD_DIR}/three20 (it will show as build/three20 when you close the dialog).
Add three20/src/Three20.bundle to your resources.
It's probably also a good idea to add the linker flags -ObjC and -all_load if you haven't already done so (same dialog, setting "Other Linker Flags").
The steps above are good enough for compiling the code. To make "Archive" work too, you will need to:
Add $(BUILT_PRODUCTS_DIR)/../three20 and $(BUILT_PRODUCTS_DIR)/../../three20 to the Header Search Paths setting. If you don't you will get compilation errors that the three20 headers cannot be found.
For all Three20 projects (so not just the top-level project), go the Build Settings and set "Skip Install" to YES. If you don't, the project will build, but you will not be able to create an IPA.
Make sure that your product name (select your target, go to Build Settings, then setting "Product Name") does not contain spaces, otherwise archiving will not work.
Finally, if you're getting errors such as ld: library not found for -lThree20 and you are using a custom build configuration (such as AdHoc), make sure to add the same build configuration to each of the Three20 projects too (just duplicate the Release configuration).
You should check out this StackOverflow question.
I've reproduced the accepted answer below:
So it looks like the easiest way
to get old Xcode 3.2 projects to work
with Xcode 4 is to do the following:
Go into Xcode 4's preferences (Cmd+,).
Select the "Locations" tab. Where it
says "Build Locations", select the
drop down and pick "Place build
products in locations specified by
targets" I'll write up a three20.info
article going into more details about
this.
Add these two paths to your header search paths in the build settings for your target:
"$(BUILD_DIR)/three20"
"$(BUILD_DIR)/../three20"
The first entry is needed for regular builds and the second entry is needed for archives.
It's that easy.
Incidentally, to create a file in your home directory with a list of all of Xcode's environment variables for your target's environement, add the following Run script build phase to your target with contents:
ENV > ~/xcode-environment.sh
Note that in this case, if your project is not making it through the compile phase of the build process, and your Run script comes later, the script won't get executed, so put it first.

Xcode: Is there a location/flag to prevent a Class from compiling?

Is there a place (or flag) in Xcode for files that you don't want to compile? There are some classes that are/may become part of a project but currently won't compile. The main project doesn't link to them but Xcode still tries to compile them. Is there a way to prevent blocking the rest of project from compiling until these new Classes are "ready"?
Note that for every source file you can specify which target(s) it belongs to - look at the inspector window for a file (Get Info) and then hit the Targets tab. If you deselect a target for a given source file then it won't be compiled as part of the build process for that target.
[This amounts to much the same thing as what Eimantas has said in his answer - it's just a different way of looking at it.]
Look for unneeded files in "Compile sources" in Target -> {AppName} branch. Remove them from there and they won't be compiled on next build (make sure to Clean before you Build again)
In Xcode 8.3.3, in the utilities window, click on File Inspector tab at the top of the window. Uncheck the file in the Target Membership area of the File Inspector. Please see image below.
You can use preprocessor statements:
#ifndef HIDE_<insert name here>
CODE
#endif
And then use:
#define HIDE_<insert name here>
above the aforementioned code in the files you don't want to compile.

Resources