Probably this is pretty simple, but I can't find a way to define a preprocessor macro for a target in Xcode 6.
I've done a screenshot to show where it is in Xcode, because it's easier :)
Select project file
Select the target you want
Go to Build Settings
Search for 'preprocessor'
Add your preprocessor macro either for Debug, Release, or both.
In Xcode 9 you have to add a preprocessor macros to Project, not Target. Also don't forget to add "D" as the firs letter. For example, "DDebug", then it works.
Related
My Xcode project has different build configurations, and they define different preprocessor macros. Autocomplete doesn't work in #ifdef blocks that are ignored by the current preprocessor flags, so I want to control which build configuration Xcode uses for autocomplete. How does Xcode decide?
After some experimenting, here's what the rules appear to be (on Xcode 13.0 beta 13A5155e):
If a configuration named Debug exists, it is used.
Otherwise, the configuration in the "Use for command line builds" menu is used.
Interestingly this entirely ignores the configuration(s) selected in the current scheme. Autocomplete still works if you delete every scheme.
I want to force Xcode to use a custom compiler ('clang-llvm' build from the src) so I can use the clang plugin. My Xcode version is 7.3.1.
People say it is possible with custom toolchains. I didn't make a research on them because easier solution worked well for me:
It is also possible to run frontend plugins directly by setting appropriate "build settings" of Xcode. (Several ways to do this, you can set them on the command line for instance: xcodebuild build FOO=bla.) Here are a few build settings that I found useful to inject C flags:
OTHER_CFLAGS, OTHER_CPLUSPLUSFLAGS or to replace the compiler(s) and linker(s):
CC, CPLUSPLUS, LD, LDPLUSPLUS, LIBTOOL
The same approach works to control the "analyze" action: CLANG_ANALYZER_EXEC, CLANG_ANALYZER_OTHER_FLAGS
Disclaimer: some of those build settings are undocumented (afaik). Use at your own risk.
(Taken from [cfe-dev] Compile/refactor iOS Xcode projects)
For me it was enough to define the following User-Defined Settings in Build Settings of Xcode projects:
CC=my-c-compiler
CXX=my-cxx-compiler
LIBTOOL=my-linker-for-static-libraries
If you use CMake, the way to inject your compiler automatically is to use
set_target_properties(your-target PROPERTIES XCODE_ATTRIBUTE_CC "${YOUR_CC}")
set_target_properties(your-target PROPERTIES XCODE_ATTRIBUTE_CXX "${YOUR_CXX}")
Couple of years ago I've written an article that addresses exactly the problem you describe: Creating and using Clang plugin with Xcode
To enable custom clang you need to actually patch internals of Xcode.app itself, it is technically doable but:
it will break when you update Xcode
it will work correctly on your machine
the version of a plugin and your compiler should match, i.e.
they should be compiled using the same tree
So in general it doesn't really scale, so be careful :)
There's a somewhat obscure feature of Xcode where it supports "alternative toolchains". For example, Swift.org provides installable toolchains for Swift built from current sources.
Unfortunately, while Apple's documentation describes how to install and use such alternative toolchains, it doesn't describe how to create them. There are scripts in the Swift source base which build a toolchain and you can look at them to figure out how it's done. They are in https://github.com/apple/swift/tree/master/utils. Start at build-toolchain, which calls build-script and go from there.
Method 1: Change the User Defined settings
Under the project or target Build Settings add the User Defined settings for
CC=/path/to/cc
CXX=/path/to/c++
This is useful if you have a single compiler or linker you want to call, or if you want to call out to a trampoline that decides what to call on the fly.
Method 2: Create a complete custom toolchain via plugin
Using Clang LLVM 1.0.xcplugin as a template (found in the Xcode.app plugins folder), you can modify the plist to point at your own alternative compiler and linker.
This OLLVM on iOS tutorial walks through it.
From project setting go to build setting with target selected. then select All beside the Basic from the top bar. then under build option you can see the compiler option.
Refer below screenshot,
Update :
I think you should refer Using C and C++ in an iOS App with Objective-C++ and this tutorial.
I have a XCode(in Swift) project with multi targets. Each target has its own macro that program can know which target is being run.
For example,
FreeVersion target has a macro "FREE"
PaidVersion target has a macro "PAID"
I would like to make test code with XCTest but it seems like XCTest source cannot be related to a specific target.
What is the best practice for using XCTest in such a situation?
Actually, the test bundle is associated with a specific app target through the TEST_HOST build setting. So I think it should be possible to set up two test targets. Each can have its own prefix header (pch) to set the macro you need, and specify their TEST_HOST.
If you duplicate your current test target, be sure to check the new build settings carefully. I've found that Xcode likes to change some settings in the duplicate but not others.
In XCode 4.5, my preprocessor macro gets ignored when defined at target level, but honored if defined at project level. It's a simple symbol definition (RESTKIT_GENERATE_SEED_DB) to be used in #ifdef. Using "levels" display, XCode shows my symbol in the resolved section. Also, I have double-checked that my current scheme is running the Debug config where macro is defined.
This was a newbie mistake. The setup was that I had two targets, one with the macro defined and one without. The reason the macro was not picked up at target level was that I never ran that target. I thought that you ran it by selecting the target in the target list under TARGETS. I realize now that in order to run a target you must select it the scheme menu next to where you select the device target.
I am trying to compile my project twice in a row, just with one preprocessor defined for the second build. (this #define is only used in one source file)
However when i call xcodebuild for the second time it rebuild everything! Using LLVM Compiler 3.0. XCode 4.2
Do you mean pre-processor flag? Whats the difference between the two builds? Just asking because sometimes there are other ways to achieve the same result.
You need to give us some more details and examples of you command lines etc.
Ijust did a quick google and checked the Xcode doco, GCC_PREPROCESSORDEFINITIONS should be GCC_PREPROCESSOR_DEFINITIONS. Xcodebuild is simply ignoring your setting because it doesn't recognise it.