# CMake goes into an infinite loop when there are two stand-alone subprojects with two different compilers within the same build

**URL:** https://discourse.cmake.org/t/cmake-goes-into-an-infinite-loop-when-there-are-two-stand-alone-subprojects-with-two-different-compilers-within-the-same-build/8833
**Category:** Code
**Tags:** os:linux, comp:gcc, comp:clang
**Created:** [August 28, 2023, 10:16pm UTC](https://discourse.cmake.org/t/cmake-goes-into-an-infinite-loop-when-there-are-two-stand-alone-subprojects-with-two-different-compilers-within-the-same-build/8833 "2023-08-28T22:16:05Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![chudur-budur](https://discourse.cmake.org/user_avatar/discourse.cmake.org/chudur-budur/32/3751_2.png) [@chudur-budur](https://discourse.cmake.org/u/chudur-budur)
#### Post date: [August 28, 2023, 10:16pm UTC](https://discourse.cmake.org/t/cmake-goes-into-an-infinite-loop-when-there-are-two-stand-alone-subprojects-with-two-different-compilers-within-the-same-build/8833/1 "2023-08-28T22:16:05Z")

</div>

I am new to `CMake`. What I want to have is two different stand-alone subprojects within one “root” project such that those two subprojects could be built independently. Given that those two subprojects use two different type of compilers.

Let’s start with the directory structure, it looks like this:

```bash
project/
├── CMakeLists.txt
├── subproject01
│ ├── CMakeLists.txt
│ └── subproject01.c
└── subproject02
    ├── CMakeLists.txt
    └── subproject02.cpp

```

The root `CMakeLists.txt` looks like this:

```cmake
cmake_minimum_required(VERSION 3.21...3.27 FATAL_ERROR)

project(project DESCRIPTION "A project")

add_subdirectory(subproject01)
add_subdirectory(subproject02)

```

Here, `subproject01` uses `gcc/g++` and the corresponding `CMakeLists.txt` looks like this:

```cmake
cmake_minimum_required(VERSION 3.21...3.27 FATAL_ERROR)

project(subproject01 DESCRIPTION "A sub-project 01")

add_library(${PROJECT_NAME} MODULE subproject01.c)

install(
    TARGETS ${PROJECT_NAME} LIBRARY DESTINATION project/subproject01
)

```

Where `subproject02` uses `clang/clang++` and the corresponding `CMakeLists.txt` file looks like this:

```cmake
cmake_minimum_required(VERSION 3.21...3.27 FATAL_ERROR)

set(CMAKE_C_COMPILER "/usr/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")

project(subproject02 DESCRIPTION "A sub-project 02")

add_library(${PROJECT_NAME} MODULE subproject02.cpp)

install(
    TARGETS ${PROJECT_NAME} LIBRARY DESTINATION project/subproject02
)

```

I can build those two subprojects independently when I invoke `cmake` command within their respective subproject directories.

But when I try to build from root project directory, the `cmake` goes into infinite loop.

How do I achieve this? I want two different stand-alone subprojects within one “root” project such that those two subprojects could be built independently or all at once from the root.

In any case, for reproducibility, `subproject01.c` looks like this:

```cpp
#include <stdlib.h>

int function(int);

int function(int a)
{
    return a + 1;
}

```

and `subproject02.cpp` looks like this:

```cpp
#include <iostream>

double function(double a)
{
    return a + 1;
}

```

---

<div class="post-metadata">

### Author: ![scivision](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/a87d85/32.png) [@scivision](https://discourse.cmake.org/u/scivision)
#### Post date: [August 29, 2023, 1:54am UTC](https://discourse.cmake.org/t/cmake-goes-into-an-infinite-loop-when-there-are-two-stand-alone-subprojects-with-two-different-compilers-within-the-same-build/8833/2 "2023-08-29T01:54:32Z")

</div>

Use ExternalProject. Working example: [GitHub - scivision/cmake-distinct-compilers: CMake project with different compilers for different directories via ExternalProject](https://github.com/scivision/cmake-distinct-compilers)
