# target\_precompile\_headers with MacOs and Objective-C++ (.mm) files

**URL:** https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205
**Category:** Code
**Tags:** os:macos, gen:ninja, gen:makefiles
**Created:** [April 24, 2021, 7:35am UTC](https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205 "2021-04-24T07:35:41Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mkloskezynaptiq](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mkloskezynaptiq/32/1404_2.png) [@mkloskezynaptiq](https://discourse.cmake.org/u/mkloskezynaptiq)
#### Post date: [April 24, 2021, 7:35am UTC](https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205/1 "2021-04-24T07:35:41Z")

</div>

Hello

When I use target\_precompile\_headers in my project the compilation of my mm-files fail with the following error:  
_error: Objective-C 1 was disabled in PCH file but is currently enabled._  
However, when I generate an Xcode - project, compilation in Xcode works fine.

I saw that target\_precompile\_headers creates the parameter -include-pch in the call of the compiler on command line. Xcode does that not! When I call the compiler myself without that -include-pch parameter on the mm-file, like Xcode does, everything works fine.  
When I use “add\_compile\_options(-Xclang -include -Xclang ${PROJECT\_SOURCE\_DIR}/stdafx.h)” instead of “target\_precompile\_headers”, it also works fine.  
My CMakeLists.txt is this:

```
cmake_minimum_required(VERSION 3.10)

project(testlib)

add_executable(testlib lib.h lib.cpp lib.mm)

#Does not work
target_precompile_headers(testlib PRIVATE stdafx.h)
#Works for my, -Xclang depends on the compiler, Cmake is using
add_compile_options(-Xclang -include -Xclang ${PROJECT_SOURCE_DIR}/stdafx.h)

```

My stdafx.h file looks like that:

```
#pragma once
#include <iostream>
#ifdef __objc__
    #import <Foundation/Foundation.h>
#endif

```

Is this a bug in CMake?

Best regards,  
Micha

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 26, 2021, 12:32pm UTC](https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205/2 "2021-04-26T12:32:07Z")

</div>

Probably. I don’t know how much Obj-C / Obj-C++ testing the PCH feature received. @brad.king Do you know?

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [April 26, 2021, 1:28pm UTC](https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205/3 "2021-04-26T13:28:11Z")

</div>

`target_precompile_headers` was added in CMake 3.16, which also added support for enabling `OBJC` and `OBJCXX` as their own languages. One needs to do that to use PCH and ObjC++ together. Try

```cmake
cmake_minimum_required(VERSION 3.16)
project(testlib LANGUAGES C CXX OBJC OBJCXX)

```

---

<div class="post-metadata">

### Author: ![mkloskezynaptiq](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mkloskezynaptiq/32/1404_2.png) [@mkloskezynaptiq](https://discourse.cmake.org/u/mkloskezynaptiq)
#### Post date: [April 26, 2021, 3:42pm UTC](https://discourse.cmake.org/t/target-precompile-headers-with-macos-and-objective-c-mm-files/3205/4 "2021-04-26T15:42:38Z")

</div>

> [@brad.king](#):
>
> `project(testlib LANGUAGES C CXX OBJC OBJCXX)`

That worked.  
Probably it was useful to add this hint to the documentation of target\_precompile\_header?

However, thanks a lot four your help,  
Micha
