# Set Definitions for external sub directory

**URL:** <https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136>\
**Category:** Code\
**Created:** [May 1, 2020, 2:57pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136 "2020-05-01T14:57:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 1, 2020, 2:57pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/1 "2020-05-01T14:57:45Z")

</div>

I am adding google test to my project as a sub directory. Is there a better way to add defines to just that subdirectory rather than…

```auto
add_definitions(-DGTEST_HAS_PTHREAD=0)
add_subdirectory(googletest)
remove_definitions(-DGTEST_HAS_PTHREAD=0)

```

---

<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:** [May 1, 2020, 5:52pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/2 "2020-05-01T17:52:19Z")

</div>

You may be able to do:

```cmake
add_subdirectory(googletest)
set_property(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/googletest" APPEND
  PROPERTY COMPILE_DEFINITIONS "-DGTEST_HAS_PTHREAD=0")

```

I don’t know if you’d need to add it to every subdirectory manually as well (you should only need it for the source directory where `add_library` is performed). If so, your example code may be the most convenient way.

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 1, 2020, 6:13pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/3 "2020-05-01T18:13:13Z")

</div>

Unfortunately, that didn’t seem to work. The googletest directory is external to our source directory if that makes a difference too. I did change the `DIRECTORY` argument to point to that directory, but it made no difference.

---

<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:** [May 1, 2020, 10:56pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/4 "2020-05-01T22:56:56Z")

</div>

Then what you have is probably the only way to do it.

---

<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:** [May 1, 2020, 10:57pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/5 "2020-05-01T22:57:35Z")

</div>

Actually, maybe you can `target_compile_definitions(googletest PRIVATE GTEST_HAS_PTHREAD=0)` after the `add_subdirectory`.

---

<div class="post-metadata">

**Author:** ![McMartin](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mcmartin/32/150_2.png) [@McMartin](https://discourse.cmake.org/u/McMartin)\
**Post date:** [May 2, 2020, 8:59am UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/6 "2020-05-02T08:59:41Z")

</div>

[https://github.com/google/googletest/blob/0eea2e9fc63461761dea5f2f517bd6af2ca024fa/googletest/CMakeLists.txt#L22](https://github.com/google/googletest/blob/0eea2e9fc63461761dea5f2f517bd6af2ca024fa/googletest/CMakeLists.txt#L22) reads:

```cmake
option(gtest_disable_pthreads "Disable uses of pthreads in gtest." OFF)

```

So you can either pass `-Dgtest_disable_pthreads=ON` when configuring, or you can write:

```cmake
set(gtest_disable_pthreads ON)
add_subdirectory(googletest)

```

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [May 2, 2020, 10:52pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/7 "2020-05-02T22:52:42Z")

</div>

> [@McMartin](#):
>
> So you can either pass `-Dgtest_disable_pthreads=ON` when configuring, or you can write:
> 
> ```auto
> set(gtest_disable_pthreads ON)
> add_subdirectory(googletest)
> 
> ```

That code example will only work reliably with CMake 3.13 or later, as it is currently written. In earlier CMake versions, the `option()` command won’t consider non-cache variables for the initial value of `gtest_disable_pthreads` where the cache variable does not exist yet. If there is no _cache_ variable by that name, the option command will use its own default and will _ignore_ any non-cache `gtest_disable_pthreads` variable when doing so. For subsequent runs, the cache variable _will_ exist and then the non-cache variable _will_ override the cache variable. It’s messy, which is why the CMake 3.13 behavior is more intuitive (`option()` uses non-cache variable if it exists and doesn’t even create a cache variable). If relying on CMake 3.13 behavior, the project should ensure it sets its minimum CMake version requirement accordingly (i.e. `cmake_minimum_required(VERSION 3.13)` or similar).

---

<div class="post-metadata">

**Author:** ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)\
**Post date:** [May 4, 2020, 1:12pm UTC](https://discourse.cmake.org/t/set-definitions-for-external-sub-directory/1136/8 "2020-05-04T13:12:05Z")

</div>

Thank you all for the input! The option route does work. It’s just that there are more defines outside of just that option that need to be set. @ben.boeckel solutions of using `target_compile_definitions` after the `add_subdirectory` worked.
