# Before or after 'project' command

**URL:** https://discourse.cmake.org/t/before-or-after-project-command/10700
**Category:** Usage
**Tags:** gen:makefiles
**Created:** [April 19, 2024, 5:53pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700 "2024-04-19T17:53:07Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![output](https://discourse.cmake.org/user_avatar/discourse.cmake.org/output/32/4512_2.png) [@output](https://discourse.cmake.org/u/output)
#### Post date: [April 19, 2024, 5:53pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700/1 "2024-04-19T17:53:07Z")

</div>

During setup of a CMake file in QtCreator for cross compiling, I ran into an issue whit took me quite some time to find out. ( Using CMake 3.29.2 )

Usually I put the project() command somewhere on top of the CMakelists.txt file.  
In this project I had some errors, which I couldn’t explain. After some trial and error, I found out that it was caused by the following sequence.

_project(myproject)_  
_set(CMAKE\_TRY\_COMPILE\_TARGET\_TYPE “STATIC\_LIBRARY”)_

This resulted in the following error:

Detecting C compiler ABI info - failed  
Arm Embedded Toolchain/10 2021.10/bin/arm-none-eabi-gcc.exe - broken

When ‘project(myproject)’ is placed **after** the set() command it works as expected.

I couldn’t find anything in the documentation which explains this.

Are there more commands/statements which are expected to be before project() ?

---

<div class="post-metadata">

### Author: ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)
#### Post date: [April 19, 2024, 6:40pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700/2 "2024-04-19T18:40:59Z")

</div>

I ran into an issue where `check_language()` didn’t work if placed _before_ the `project()` command. I was convinced it was broken until I stumbled on the order dependency when trying to create a [MVRE](https://stackoverflow.com/help/minimal-reproducible-example).

I don’t have an answer for you but I feel your pain.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 19, 2024, 10:47pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700/3 "2024-04-19T22:47:42Z")

</div>

Anything that relates to setting up your toolchain should go in a toolchain file. That toolchain file will be read as part of the `project()` command. Critically, it is also used when CMake creates a private sub-build to test the toolchain whenever a new language is enabled (by default, the first `project()` call will enable the C and CXX languages unless you tell it otherwise).

In your case, `CMAKE_TRY_COMPILE_TARGET_TYPE` needs to be set in a toolchain file. It controls how CMake tests the toolchain, so it has to be set before any languages are enabled. This is why when you tried to set it after `project()` it was having no effect. Resist the urge to set it within your project’s `CMakeLists.txt` file. It looks like you’re cross-compiling with an embedded toolchain, and for that you basically have to use a toolchain file to get the right behavior.

You might find it worthwhile to read the [cmake-toolchains manual](https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html) to get a bit of an introduction to some of these topics.

---

<div class="post-metadata">

### Author: ![output](https://discourse.cmake.org/user_avatar/discourse.cmake.org/output/32/4512_2.png) [@output](https://discourse.cmake.org/u/output)
#### Post date: [April 20, 2024, 2:41pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700/4 "2024-04-20T14:41:40Z")

</div>

Thanks for your reply 😃  
This was indeed a part which was not in the toolchain file. Its indeed listed on the toolchain file page.

Small sidenote, I also have your book, which is really nice. I was looking for this in the toolchain file section where it was not mentioned. And where basically is stated put in as little as possible. Maybe small improvement for the next print.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [April 20, 2024, 10:46pm UTC](https://discourse.cmake.org/t/before-or-after-project-command/10700/5 "2024-04-20T22:46:01Z")

</div>

`CMAKE_TRY_COMPILE_TARGET_TYPE` is discussed in _Section 24.5: Compiler Checks_ of the _Toolchains And Cross Compiling_ chapter. The paragraph that mentions it doesn’t specifically talk about toolchain files, but it follows a paragraph that does. I can look at making it clearer that it would also normally only be set in a toolchain file.
