How to include subdirectory with a different generator?

Hello,

I have two working CMakeList.txt files -

<project root>CMakeList.txt


#[[

# build with shared prebuilt SDL3 library (DLL)

cmake -B build -G "Ninja Multi-Config"

cmake --build build

build\Debug\hello.exe

cmake --build build --config Release

build\Release\hello.exe

]]

cmake_minimum_required(VERSION 3.20)

project(hello)

set(CMAKE_C_COMPILER "C:/Program Files/LLVM/bin/clang.exe")

set(CMAKE_CXX_COMPILER "C:/Program Files/LLVM/bin/clang++.exe")

add_library(SDL3 SHARED IMPORTED)

set_target_properties(SDL3 PROPERTIES

INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/SDL3-3.4.2/include"

IMPORTED_IMPLIB "${CMAKE_SOURCE_DIR}/SDL3-3.4.2/lib/x64/SDL3.lib" # SHARED

)

add_executable(${PROJECT_NAME} hello.cpp)

target_link_libraries(${PROJECT_NAME} SDL3)

#set(ENV{CMAKE_GENERATOR} Ninja)

#add_subdirectory(emscripten)

which builds a SDL3 Hello, World! C++ program using generator (-G) Ninja Multi-Config and

<project root>/emscripten/CMakeList.txt


#[[

cmake -B build -G Ninja

cmake --build build

python -m http.server 8000

http://localhost:8000

]]

cmake_minimum_required(VERSION 3.21)

set(CMAKE_TOOLCHAIN_FILE "../../emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake")

project(hello_wasm)

set(CMAKE_BUILD_TYPE MinSizeRel)

# Set the name of the future application (in Windows this would be app.exe, while for web it will be app.js / app.wasm)

add_executable(${PROJECT_NAME} ../hello.cpp)

# Tell CMake where to find the SDL3 library configuration files

set(SDL3_DIR install_libSDL3/lib/cmake/SDL3)

# Load SDL3 package settings (compilation parameters and paths to headers)

find_package(SDL3 REQUIRED)

# Link SDL3 to our application (configures linking and include paths)

target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)

which builds SDL3 Hello, World! as emscripten (javascript and WASM files) from the same C++ source file using generator (-G) Ninja

I want to control the entire build from a single CMakeList.txt, so I thought to trigger emscripten build from the end of the C++ build by adding the following to the end of <project root>CMakeList.txt


set(ENV{CMAKE_GENERATOR} Ninja)

add_subdirectory(emscripten)

However it fails with errors -


CMake Error at emscripten/CMakeLists.txt:23 (find_package):

Could not find a configuration file for package "SDL3" that is compatible

with requested version "".

The following configuration files were considered but not accepted:

<project root>emscripten/install_libSDL3/lib/cmake/SDL3/SDL3Config.cmake, version: 3.4.2 (32bit)

The version found is not compatible with the version requested.

I also tried adding add_subdirectory(emscripten) to the end of <project root>CMakeList.txt and building with cmake - B build -G Ninja, i.e the same generator for both CMakeList.txt files but it gave the same error.

Is there a way I can control the entire build from a main CMakeList.txt?

After further research, CMake seems to expect a single build system per invocation. In other words, add_subdirectory does not support a different generator, toolchain or compilers from the calling CMakeLists.txt. Can anyone confirm this?

If you want to build with several toolchains, you can use a CMake project that wraps each sub- build as ExternalProject.

I have a working master CMakeLists.txt

cmake_minimum_required(VERSION 3.23)

project(checkers)
add_subdirectory(src)

where directory src contains my cpp files and their associated CMakeLists.txt. I successfully generated the build system with

cmake -B build -DCMAKE_CXX_COMPILER=clang++ -DSDL_STATIC=ON -DSDL_SHARED=OFF -G "Ninja Multi-Config"

Now I want adapt the master CMakeLists.txt to build external projects

cmake_minimum_required(VERSION 3.23)

project(checkers)

include(ExternalProject)

ExternalProject_Add(checkers_cpp
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src
CMAKE_ARGS -DCMAKE_CXX_COMPILER=clang++; -DSDL_STATIC=ON; -DSDL_SHARED=OFF
CMAKE_GENERATOR "Ninja Multi-Config"
)

but received error:

The current configuration mixes Clang and MSVC or some other CL compatible
    compiler tool.  This is not supported.  Use either Clang or MSVC as the
    compiler for all of C, C++, and/or HIP.

I suspect the error comes from my CMAKE_ARGS options. I have tried with and without quotes but it did not help. Can anyone see where I went wrong?

Shane

By default CMake enabled C and CXX.
Specify LANGUAGES CXX in your project call, if you only want to use C++.

I added the language to the project command -

project(checkers LANGUAGES CXX)

but it still fails. See full cmake output below. Any ideas?

(3.13) PS C:\Users\sss\dev\SDL3\checkers> cmake -B build     
-- Building for: Visual Studio 18 2026
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.26200.
-- The C compiler identification is MSVC 19.50.35728.0
-- The CXX compiler identification is MSVC 19.50.35728.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: d:/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.50.35717/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: d:/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.50.35717/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.5s)
-- Generating done (0.0s)
-- Build files have been written to: C:/Users/sss/dev/SDL3/checkers/build
(3.13) PS C:\Users\sss\dev\SDL3\checkers> cmake --build build
MSBuild version 18.4.0+6e61e96ac for .NET Framework

  1>Checking Build System
  Creating directories for 'checkers'
  Building Custom Rule C:/Users/sss/dev/SDL3/checkers/CMakeLists.txt
  No download step for 'checkers'
  No update step for 'checkers'
  No patch step for 'checkers'
  Performing configure step for 'checkers'
  -- The C compiler identification is MSVC 19.50.35728.0
  -- The CXX compiler identification is Clang 22.1.1 with GNU-like command-line
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Check for working C compiler: D:/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.50.35717/bin/
  Hostx64/x64/cl.exe - skipped
  -- Detecting C compile features
  -- Detecting C compile features - done
  CMake Error at C:/Program Files/CMake/share/cmake-4.3/Modules/Platform/Windows-Clang.cmake:187 (message):
    The current configuration mixes Clang and MSVC or some other CL compatible
    compiler tool.  This is not supported.  Use either Clang or MSVC as the
    compiler for all of C, C++, and/or HIP.
  Call Stack (most recent call first):
    C:/Program Files/CMake/share/cmake-4.3/Modules/Platform/Windows-Clang.cmake:196 (__verify_same_language_values)
    C:/Program Files/CMake/share/cmake-4.3/Modules/Platform/Windows-Clang-CXX.cmake:1 (include)
    C:/Program Files/CMake/share/cmake-4.3/Modules/CMakeCXXInformation.cmake:48 (include)
    CMakeLists.txt:16 (project)
  
  
  -- Configuring incomplete, errors occurred!
d:\Program Files\Microsoft Visual Studio\18\Community\MSBuild\Microsoft\VC\v180\Microsoft.CppCommon.targets(254,5): err
or MSB8066: Custom build for 'C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\894992f8c63144c2b6569698e9070c33\checke
rs-mkdir.rule;C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\894992f8c63144c2b6569698e9070c33\checkers-download.rule
;C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\894992f8c63144c2b6569698e9070c33\checkers-update.rule;C:\Users\sss
\dev\SDL3\checkers\build\CMakeFiles\894992f8c63144c2b6569698e9070c33\checkers-patch.rule;C:\Users\sss\dev\SDL3\checke
rs\build\CMakeFiles\894992f8c63144c2b6569698e9070c33\checkers-configure.rule;C:\Users\sss\dev\SDL3\checkers\build\CMa
keFiles\894992f8c63144c2b6569698e9070c33\checkers-build.rule;C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\894992f8
c63144c2b6569698e9070c33\checkers-install.rule;C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\a64faf56fd3aea4aaeca02
d0bd1f2afb\checkers-complete.rule;C:\Users\sss\dev\SDL3\checkers\build\CMakeFiles\8405834ee13b11410e6fbd86de8364be\ch
eckers.rule;C:\Users\sss\dev\SDL3\checkers\CMakeLists.txt' exited with code 1. [C:\Users\sss\dev\SDL3\checkers\buil
d\checkers.vcxproj]

Your log still says that the C compiler is detected, for your super project and for your external project.

So you maybe have changed the wrong file, maybe you need a fresh configure (simply remove your build directory).

In the cmake output above, I did delete the build directory before running cmake -B build followed by cmake --build build

Can you add a --trace to the CMAKE_ARGS, so the log clearly show the commands used.

If it’s really just CXX and the C compiler still gets detected, I would consider this a bug of the Visual Studio generator.
I don’t use VS, so I do not have experience with it, but I can’t find a documented exception to the normal behavior.

I added --trace to the master CMakeLists.txt -

cmake_minimum_required(VERSION 3.23)

project(checkers LANGUAGES CXX)

include(ExternalProject)

ExternalProject_Add(checkers_cpp
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/src
CMAKE_ARGS -DCMAKE_CXX_COMPILER=clang++ --trace; -DSDL_STATIC=ON; -DSDL_SHARED=OFF
CMAKE_GENERATOR "Ninja Multi-Config"
)

Trace (too large for discourse.cmake.org) : MSBuild version 18.4.0+6e61e96ac for .NET... - Pastes.io

At the very top of your log it reports the project call looks like this:

C:/Users/sss/dev/SDL3/checkers/src/CMakeLists.txt(16):  project(checkers_cpp )

Please confirm from the command line, what that line really looks like, e.g. TYPE C:\Users\sss\dev\SDL3\checkers\src\CMakeLists.txt