# "add\_subdirectory twice", with a different environment variable value for each target

**URL:** https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376
**Category:** Code
**Created:** [April 5, 2022, 1:53pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376 "2022-04-05T13:53:26Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Prosperoh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prosperoh/32/2324_2.png) [@Prosperoh](https://discourse.cmake.org/u/Prosperoh)
#### Post date: [April 5, 2022, 1:53pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/1 "2022-04-05T13:53:26Z")

</div>

Hi all!

I have an external submodule that I want to include in my project. In its own CMakeLists.txt it uses the value of an environment variable to define some macros and include some modules. The user is expected to define this environment variable before using add\_subdirectory.

I have two different targets (executables) in my project for which the submodule should be included, but for each target with a different environment variable value. Similar to being able to “use add\_subdirectory twice, once per target”. But it seems that I can only “use add\_subdirectory once”.

Thank you, all the best!

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 5, 2022, 3:21pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/2 "2022-04-05T15:21:04Z")

</div>

you may use this form:

```cmake
add_subdirectory(subdir ${CMAKE_BINARY_DIR}/variant1)
add_subdirectory(subdir ${CMAKE_BINARY_DIR}/variant2)

```

Do you need to set a `set(CMAKE_VAR ...)` or really on `$ENV{VAR}` before?

---

<div class="post-metadata">

### Author: ![Prosperoh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prosperoh/32/2324_2.png) [@Prosperoh](https://discourse.cmake.org/u/Prosperoh)
#### Post date: [April 5, 2022, 3:22pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/3 "2022-04-05T15:22:55Z")

</div>

Just a `set(CMAKE_VAR ...)`, my bad.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 5, 2022, 3:29pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/4 "2022-04-05T15:29:32Z")

</div>

**Than I would try it indirect:**

- create 2 subdirectories with a simple `CMakeLists.txt` as proxy to the extern subdirs as above.
- In this `CMakeLists.txt` set the needed variable before `add_subdirectory()`

---

<div class="post-metadata">

### Author: ![Prosperoh](https://discourse.cmake.org/user_avatar/discourse.cmake.org/prosperoh/32/2324_2.png) [@Prosperoh](https://discourse.cmake.org/u/Prosperoh)
#### Post date: [April 6, 2022, 9:06am UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/5 "2022-04-06T09:06:30Z")

</div>

Though I’m not sure if I understood correctly, I tried something along these words and CMake complains about the duplicate library name of the module I’m trying to use.

It might be easier if I just give the module I’m using and its CMakeLists.txt:

> <https://github.com/itay-grudev/SingleApplication/blob/master/CMakeLists.txt>

EDIT: The CMake variable I have to define is `QAPPLICATION_CLASS`.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 6, 2022, 12:34pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/6 "2022-04-06T12:34:16Z")

</div>

it works on `OSX` with `brew`:

```bash
bash-3.2$ cmake -B build -S . -G Ninja
# ...
bash-3.2$ cmake --build build --target all
[0/7] Re-running CMake...
-- CPM: adding package SingleApplication@3.3.4 (v3.3.4 at /Users/clausklein/.cache/CPM/singleapplication/23aa3f1b96d47fedade6f45b8b73e103ad2a2fb8)
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/clausklein/build
[7/7] Linking CXX executable basic
bash-3.2$ 

```

```cmake
cmake_minimum_required(VERSION 3.21...3.23)

project(basic LANGUAGES CXX)

# ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info

include(cmake/CPM.cmake)

set(Qt5_DIR /usr/local/Cellar/qt@5/5.15.3/lib/cmake/qt5)
find_package(Qt5 COMPONENTS Core REQUIRED)

# SingleApplication base class
CPMAddPackage(
  NAME QCoreApplication
  GIT_TAG v3.3.4
  GITHUB_REPOSITORY itay-grudev/SingleApplication
  OPTIONS "QAPPLICATION_CLASS QCoreApplication"
  #or OPTIONS "QAPPLICATION_CLASS QApplication"
)

add_executable(basic ${SingleApplication_SOURCE_DIR}/examples/basic/main.cpp)

target_link_libraries(basic SingleApplication::SingleApplication)

```

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 6, 2022, 1:00pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/7 "2022-04-06T13:00:44Z")

</div>

This shows how to compile a QT5 application only with a single `CMakeLists.txt.` (no sources"!)  
P.S.: it is a `cmake project` packages, not a `git submodule` 🕶

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 6, 2022, 5:33pm UTC](https://discourse.cmake.org/t/add-subdirectory-twice-with-a-different-environment-variable-value-for-each-target/5376/8 "2022-04-06T17:33:49Z")

</div>

This may be the second variant:

```cmake
cmake_minimum_required(VERSION 3.21...3.23)

project(calculator LANGUAGES CXX)

# ---- Add dependencies via CPM ----
# see https://github.com/TheLartians/CPM.cmake for more info

include(cmake/CPM.cmake)

set(Qt5_DIR /usr/local/Cellar/qt@5/5.15.3/lib/cmake/qt5)
find_package(Qt5 COMPONENTS Core REQUIRED)

set(CMAKE_AUTOMOC ON)

# SingleApplication base class
CPMAddPackage(
  NAME QApplication
  #or NAME QCoreApplication
  GIT_TAG v3.3.4
  GITHUB_REPOSITORY itay-grudev/SingleApplication
  #or OPTIONS "QAPPLICATION_CLASS QCoreApplication"
  OPTIONS "QAPPLICATION_CLASS QApplication"
)

#or add_executable(basic ${SingleApplication_SOURCE_DIR}/examples/basic/main.cpp)

add_executable(${PROJECT_NAME}
    ${SingleApplication_SOURCE_DIR}/examples/calculator/button.h
    ${SingleApplication_SOURCE_DIR}/examples/calculator/calculator.h
    ${SingleApplication_SOURCE_DIR}/examples/calculator/button.cpp
    ${SingleApplication_SOURCE_DIR}/examples/calculator/calculator.cpp
    ${SingleApplication_SOURCE_DIR}/examples/calculator/main.cpp
)

target_include_directories(${PROJECT_NAME} PUBLIC ${SingleApplication_SOURCE_DIR}/examples/calculator)
target_link_libraries(${PROJECT_NAME} SingleApplication::SingleApplication)

```
