问题
I have an Objective C project in XCode. When building, XCode precompiles my prefix PCH file twice. The first time, it uses the arguments -x objective-c-header
, and it works fine. The second time, it uses -x c-header
, and it fails (due to the file containing Objective C code).
I have another project that's set up the same, but it builds fine. (The PCH is only precompiled once, with Objective C.) I compared the build settings for the two projects side-by-side, and nothing relevant differed. I compared the build settings for the targets side-by-side, and again, no relevant differences.
I tried cleaning all targets and rebuilding, but no luck. I tried restarting XCode, but no luck. I grepped my project folder for c-header
, but it only showed up in the build logs.
What could be causing this?
回答1:
The PCH file is being rebuilt multiple times because there is multiple languages in your project; once with the objective-c compiler and once with the straight C file, thus that you had Objective-C code outside of the OBJC #ifdef and that caused the straight C compiler to hoark up it's lunch.
If you had a C++ file, you'd probably end up with 3 PCH compilations (IIRC -- been a while since I mucked with that).
回答2:
I hate answering my own question, but I figured it out. I disabled precompiling the prefix header to see if that would let me continue working. The build still failed with the same error, but it failed when including the file from a .c file I forgot I had in the project. Apparently, the existence of this .c file is the difference between this project and the other. Now that I know what's going on, I moved my code inside the #ifdef __OBJC__
, and everything seems fine.
来源:https://stackoverflow.com/questions/4091489/xcode-precompiling-pch-twice-second-time-with-wrong-language