"add_subdirectory twice", with a different environment variable value for each target

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!

you may use this form:

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?

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

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()

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:

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

it works on OSX with brew:

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_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)

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 :sunglasses:

This may be the second variant:

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)