135

When I try to build my app in Xcode, I get this error message:

PCH file built from a different branch ((clang-425.0.24)) than the compiler ((clang-425.0.27))

It doesn't happen before, but this was the first build after updating Xcode.

Other apps work, but not this specific one.

If I turn off the "Precompile Prefix Header" setting, it works.

How do I fix this error and still keep that setting on?

spajce
  • 7,044
  • 5
  • 29
  • 44
Macro206
  • 2,143
  • 3
  • 19
  • 25

22 Answers22

249

This is often a caching problem. Usually it can be resolved by holding down the Option key and choosing Product > Clean Build Folder...

ThomasW
  • 16,981
  • 4
  • 79
  • 106
gaige
  • 17,263
  • 6
  • 57
  • 68
  • My Savior! (aside: Seemingly SO has a minimum content length for comments. I suppose an up vote is enough thanks but it seems so much less personal.) – Ralphleon Mar 17 '13 at 19:49
  • 4
    This did not fix the error for me. I've also performing a full clean and deleting the derived data. – Ben S Mar 18 '13 at 17:42
  • That is part of why the answer says that it can usually be resolved by doing **Clean Build Folder...**. I have run into some situations where Xcode has strange ideas of where my build product and partials lie and it requires a more manual clear out. In those cases, I have always ended up tracking the true locations down in the log information and deleting the locations pointed at by the actual compiler error log. – gaige Mar 18 '13 at 22:02
  • 6
    Worked for me, except it was under `Product` > `Clean` and no need for the command key. (Xcode 4.6.1) – Steve Haley Mar 20 '13 at 17:35
  • Thanks! Worked for me too, this happened to me after I upgrade xcode to 4.6.2 – ewiinnnnn Apr 17 '13 at 04:01
  • 1
    @SteveHaley There is a `Product > Clean`, but it does a less thorough job. Some people can get buy with `Product > Clean`, for most `Product > Clean Build Folder...` is sufficient, and others need the manual removal as indicated in @Sedes answer below. – gaige Apr 19 '13 at 12:05
  • Make clean == Product > Clean. Nice. Thanks. – MoFlo Apr 30 '13 at 19:40
  • 7
    I know its old question, but the above mentioned fix did not help me. Just by deleting Derived Data and building the project again work fine. – Ankit Jain May 05 '15 at 12:00
  • 2
    can you just do rm -rf /ios/build? – SuperUberDuper Feb 06 '17 at 12:43
  • Yep, nothing helped until I just deleted the build output dir in the project root as SuperUberDuper suggested. – RAM237 Feb 05 '20 at 10:08
48

Delete the DerivedData folder for the project. Look under Xcode preferences -> Locations to see where you save it.

Mr-IDE
  • 7,051
  • 1
  • 53
  • 59
wcochran
  • 10,089
  • 6
  • 61
  • 69
  • It's important to note that `DerivedData` is not necessarily where your `Build` folder is, this helped me a lot, thanks! – Etheryte Dec 28 '16 at 06:34
  • 1
    delete `moduleCache` inside of `DerivedData` too – whtlnv Jan 16 '18 at 00:11
  • You should first try to "clean" as the other answers say. When this doesn't work, I would simply just recursively delete the entire `DerivedData` folder ... "nuke from orbit ... only way to be sure." @whitelionV this would also rm `moduleCache` and everything else in there. – wcochran Jan 16 '18 at 18:30
39

It seems that the Product > Clean Build Folder... (with Option key pressed) works for most people. See the selected answer by @gaige.

If you're unlike most people (myself included) and this still causes you trouble XCode has likely left your shared precompiled headers elsewhere. For me they were in a folder similar to this:

/var/folders/<some random two characters>/<some random string>/C/com.apple.Xcode.502/SharedPrecompiledHeaders

I just deleted the entire SharedPrecompiledHeaders folder and that solved things for me. I wouldn't go touching much else in here though...

Gabriel Jensen
  • 4,082
  • 1
  • 17
  • 14
Sedes
  • 563
  • 6
  • 11
  • 2
    This fixed this problem for me. For me it is because I have a command-line driven build. – ThomasW Mar 21 '13 at 02:10
  • 2
    If this is your case you can find that folder in the Xcode build transcript. I looked for the string "SharedPrecompiledHeaders" – José Manuel Sánchez Apr 19 '13 at 13:13
  • 6
    Yes, this is the right solution. Specially if you build from command line or you have a build script. The precompiled header location can be found in the build settings, "Build locations"->Precompiled headers cached path. – karim Apr 24 '13 at 08:55
  • How do you find the var directory? – GoldenJoe Dec 09 '14 at 08:24
  • 1
    @GoldenJoe Open Finder, `Shift-Cmd-G` and type `/var` – Sedes Dec 09 '14 at 22:06
  • Did not work for me. Using Shift-Cmd-G Finder found the /var folder, but searching for SharedPrecompiledHeaders found nothing. Yes, I typed it in correctly. – Alyoshak Dec 09 '15 at 18:18
25

Go to Xcode Preferences->Locations (as mentioned in the answer by wcochran) and simply rename your DerivedData folder. I simply changed mine from "DerivedData" to "DerivedData2" and that forced the system to stop referencing the old location. This fixed it for me when none of the other answers worked. See pic:

enter image description here

Alyoshak
  • 2,696
  • 10
  • 43
  • 70
18

None of the other options worked for me, here is what fixed it:

I deleted the cache folder at /Users/(Yourname)/Library/Developer/Xcode/DerivedData/ModuleCache/(renaming it works too).

Try deleting the folder mentioned in the error, if your error looks something like this:

PCH was compiled with module cache path '/Users/User/Library/Developer/Xcode/DerivedData/ModuleCache/75YIWZVTEAO8', but the path is currently '/Users/Stan/Library/Developer/Xcode/DerivedData/ModuleCache/75YIWZVTEAO8

Stan Tatarnykov
  • 691
  • 9
  • 17
  • Yes, you are right. None of the above works for me. Just delete the folder it mentioned, and rebuild, that's it. – Zhang Buzz May 29 '17 at 14:57
  • This is the correct answer. Clean/delete DerivedData doesn't not work for me after moving the project to other path. – Bill Chan Apr 30 '20 at 18:55
  • 1
    This worked for me with a command line build. For anyone stumbling upon this, deleting the second path is what you need to do (the one the error says is currently the path). – Dominic Williams Jan 04 '21 at 16:39
14

In your ios project directory, go to build and delete ModuleCache folder. Run react-native run-ios again, it should work as expected. Summary - rm -rf ios/build/ModuleCache

Ritik Rishu
  • 159
  • 2
  • 4
  • Yep, also on react-native and build works from Xcode but not from CLI using npm run ios. Removing cache as suggested solved the problem. – dakt Oct 11 '18 at 08:54
13

Deleting the Build and DerivedData folders from the project location in Finder worked for me.

spfursich
  • 5,045
  • 3
  • 21
  • 24
7

I met this error when trying to build a project with Swift Package Manager.

The solution is just delete the .build directory.

Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
6

After upgrading Xcode I was getting this with multiple projects. Clean Build Folder and Clean had no effect - @sedes answer and @josema.vitaminew 's comment did it for me. But it can be tricky unless you use a bit of cunning, so here's a step by step shortcut:

  1. go to the build log that failed
  2. use cmd-f "SharedPrecompiledHeaders" to find it (it will be in the middle of a massive horrible complex commandline)
  3. drag-select the text "SharedPrecompiledHeaders" and all the characters BEFORE but NOT AFTER it until you get to the start of that path bit (still in the middle of the big block of text) e.g. for me: "/var/folders/03/n71d4r551jv40j5nb8r9fjy80000gn/C/com.apple.Xcode.501/SharedPrecompiledHeaders
  4. open Finder
  5. menu: Go -> Go to folder...
  6. copy/paste the selected path
  7. in "SharedPrecompiledHeaders" delete ALL sub-folders (this is safe: it is only cached data)

...and now all your projects will be "Fixed", but will have to re-build themselves first time with zero caching (so the NEXT build ONLY will be slower than normal).

Adam
  • 32,900
  • 16
  • 126
  • 153
5

Specially if you build from command line or you have a build script, cleaning xcode target is not enough. You have delete this folder.

The precompiled header location can be found in the

Target -> build settings => "Build locations" -> Precompiled headers cached path

Delete this folder with,

#rm -fr SharedPrecompiledHeaders

enter image description here

karim
  • 15,408
  • 7
  • 58
  • 96
  • My cache path was in the build folder /build/ModuleCache/. Removing it from the old location didn't do the trick – TheJeff Aug 06 '17 at 14:55
5

in my case I got rid of all files under SharedPrecompiledHeaders this way and everything started to work fine:

sudo find /var/folders/ -type d -name "SharedPrecompiledHeaders" | xargs -n 1 -I dir sudo find dir -name \* -type f -delete
Hamid Tavakoli
  • 4,567
  • 1
  • 33
  • 34
  • This is something I can easily use in a script, so I like it... I haven't discovered any bad cases [yet] – Matt Oct 16 '14 at 19:56
4

For me this would always happen when I copy my project to another location. I'd do this to push it into a repo or something.

I have to delete the Build folder (rm -rf Build/) in the old and new locations to get it working. It makes sense because the Build folder will have data that is irrelevant to the new location.

Cleaning Derived Data or deleting the sharedPrecompiledHeaders folders didn't really do it in this case.

TheJeff
  • 3,665
  • 34
  • 52
3

If it's not solved by cleaning. It's a build cache problem - hold down Option key and navigate to Product > Clean Build Folder. That fixed my problem.

ICL1901
  • 7,632
  • 14
  • 90
  • 138
Vardhan
  • 965
  • 2
  • 11
  • 21
3

In my case Xcode was creating the DerivedData folder in my project main bundle(/Users/Path_to_your_project_source_code/YourProjectName/DerivedData)

I just deleted this folder from my source code bundle and now it is working.

To check Go to Xcode Preferences->Locations and see if DerivedData is selected to "Relative". See the picture below enter image description here

2

you should holding down "shift + command + C",and then you will build succeed

Peter Zhu
  • 51
  • 4
2

You may try cleaning the product or the build folder:

SHIFT + COMMAND + K

OPTION + SHIFT + COMMAND + K

Mateus
  • 2,640
  • 5
  • 44
  • 62
2

Cleaning and/or deleting derived data did not work for me. What did work is touching/saving the .pch file (add a character, then delete it) to force it to be rebuilt. This did the trick.

Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74
2

If doing a clean build does not work for you, look for the PCH files in /var/folders/../SharedPrecompiledHeaders and remove the PCH files, which worked for me.

Jonathan Lin
  • 19,922
  • 7
  • 69
  • 65
2

UPDATED

This worked perfectly for me:

  • Close your project or workspace.
  • In Finder: ⇧shift+⌘cmd+G
  • Paste: ~/Library/Developer/Xcode/DerivedData/
  • Delete the ModuleCache folder and empty trash.
  • Open up your project.
  • Clean: ⇧shift+⌘cmd+K
  • Build: ⌘cmd+B

xCode 7.3.1
reference here

2

If you are doing a command line build via xcodebuild, then see if the -derivedDataPath BUILD_DIR option is used. If so, your ModuleCache.noindex directory will be in the BUILD_DIR instead of the normal Xcode location. Delete the ModuleCache.noindex directory and you will be all set.

I ran into this situation because I am building a React Native app, and the iOS client is typically built from the command line via the React Native toolset.

phatmann
  • 18,161
  • 7
  • 61
  • 51
1

I've tried cleaning the project and cleaning the build folder which both didn't work.

After that, I quit xCode completely, deleted the derived data folder at its standard location: ~/Library/Developer/Xcode/DerivedData, started xCode again and built the project.

That seemed to have resolved the problem for me.

Ramsy de Vos
  • 1,053
  • 15
  • 21
  • Could you elaborate a bit please ? – Richard Dally Dec 10 '15 at 09:01
  • Closing Xcode, running `rm -rf ~/Library/Developer/Xcode/DerivedData` in Terminal, and then opening Xcode did it for me. I also made sure my locations (Xcode -> Preferences -> Locations) were all default. – sman591 Feb 25 '16 at 17:49
0

I just deleted DerivedData and worked for me.

Obsidian
  • 3,719
  • 8
  • 17
  • 30
KAUSHIK PARMAR
  • 535
  • 6
  • 11