# Is it possible to use CMake with and without a compile value?

**URL:** https://discourse.cmake.org/t/is-it-possible-to-use-cmake-with-and-without-a-compile-value/6433
**Category:** Usage
**Tags:** os:windows
**Created:** [September 7, 2022, 11:08pm UTC](https://discourse.cmake.org/t/is-it-possible-to-use-cmake-with-and-without-a-compile-value/6433 "2022-09-07T23:08:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [September 7, 2022, 11:08pm UTC](https://discourse.cmake.org/t/is-it-possible-to-use-cmake-with-and-without-a-compile-value/6433/1 "2022-09-07T23:08:22Z")

</div>

I don’t know how to title this…

We have a C code base `foo` that defaults to creating `foo with networking` but if the preprocessor flag NO\_NETWORKING is defined it creates `foo no networking`.

We use Visual Studio.

What the team wants is to run Cmake once and have a solution with the targets:

1. foo
2. fooNoNetwork

Is this possible? And if so how?

The first solution I tried was having a top level CMakeLists.txt file that basically does

```auto
# invoke default
add_subdirectory(foo)

# build no-network
set(NO_NETWORK 1)
add_subdirectory(foo)

```

Where the `CMakeLists.txt` in foo already has appropriate logic such it will create targets and compile defines based on the existence of NO\_NETWORK.

This works on a single basis.

```auto
# invoke default
add_subdirectory(foo)

```

gives the desired results for foo

and

```auto
# build no-network
set(NO_NETWORK 1)
add_subdirectory(foo)

```

gives the desired results for fooNoNetwork.

However, when I do them combined as indicated above, CMake fails stating the subdirectory has already been added.

* * *

Is what I want possible? If so can you please teach me? If it’s not possible, are there suggestions you can share for how to achieve this?

One thought I have is to break the networking logic out into two sub libraries.

foo-with-networking.lib  
foo-no-networking.lib

and then the executables are achieve by linking with the appropriate sub libraries.

However, the team would prefer to keep the existing solution as is without this library re-write. So I’m exploring that first.

Thanks in advance.

---

<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: [September 8, 2022, 2:33pm UTC](https://discourse.cmake.org/t/is-it-possible-to-use-cmake-with-and-without-a-compile-value/6433/2 "2022-09-08T14:33:13Z")

</div>

> [@JRR](#):
>
> Is what I want possible? If so can you please teach me? If it’s not possible, are there suggestions you can share for how to achieve this?

You can pass an explicit build directory as a second argument. This should work:

```cmake
add_subdirectory(foo)

set(NO_NETWORK 1)
add_subdirectory(foo "${CMAKE_CURRENT_BINARY_DIR}/foo-no-network")

```
