Xcode generator does not support multi-language precompiled headers
Attached is a small project containing .c, .cpp, .m, and .mm source files where target_precompile_headers() is used to precompile a single prefix.h.
The generated Xcode project contains:
GCC_PRECOMPILE_PREFIX_HEADER = YES
GCC_PREFIX_HEADER = .../CMakeFiles/Test.dir/Debug/cmake_pch.hxx
The GCC_PREFIX_HEADER header is compiled by Xcode once for each language in the project:
clang -x c-header -target ...
clang -x c++-header -target ...
clang -x objective-c-header -target ...
clang -x objective-c++-header -target ...
However the cmake_pch.hxx header only includes its contents when __cplusplus is defined:
#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS
#ifdef __cplusplus
#include "/Users/test/Desktop/xcode_pch/prefix.h"
#endif // __cplusplus
#endif // CMAKE_SKIP_PRECOMPILE_HEADERS
This means that prefix.h is only visible to C++ and Objective-C++ source.
As Xcode's GCC_PREFIX_HEADER setting only takes a single path the Xcode generator should generate a single language-agnostic header:
GCC_PRECOMPILE_PREFIX_HEADER = YES
GCC_PREFIX_HEADER = .../CMakeFiles/Test.dir/Debug/cmake_pch.h
#ifndef CMAKE_SKIP_PRECOMPILE_HEADERS
#include "/Users/test/Desktop/xcode_pch/prefix.h"
#endif // CMAKE_SKIP_PRECOMPILE_HEADERS
Xcode will then generate per-language builds of this precompiled header itself, passing the correct build to each source file as required.