conan-io / conan-io/docs

[question] Issue with `extra_cxxflags` not initializing `CMAKE_CXX_FLAGS_INIT` in Ninja Multi-Config CMake projects since Conan 2.1.0

Open
#3,660 6 comments 0 reactions 1 assignee Claimed by @memsharded View on GitHub
Dominant language
C++
Stars
125
Forks
382
Avg merge
22h 39m
Merged PRs (30d)
21

Description

### What is your question?

I have a Ninja Multi-Config CMake project whose Conan recipe has a `build()` method that configures and builds the project. Before Conan 2.1.0, I could build the project for different configurations using the following commands:

1. `conan build -s:h build_type=Release`
2. `conan build -s:h build_type=Debug`

These commands could be run in any order, and the second command would work correctly.

However, after upgrading to Conan 2.1.0, the second command fails due to missing C++ flags. Upon investigation, I found that the commit https://github.com/conan-io/conan/commit/7d93ccc5c77b9c5829162c11b52bdbaaf3e9bcb6, introduced by https://github.com/conan-io/conan/pull/15654, changed the behavior of `extra_cxxflags`.

Instead of initializing `CMAKE_CXX_FLAGS_INIT`, the `extra_cxxflags` now initialize `CMAKE_CXX_FLAGS__INIT`. This means that when my recipe's `build()` method immediately configures CMake, it only sets up the flags for the first build configuration, and the subsequent configurations do not have access to the desired flags.

My project sets up `extra_cxxflags` in the `conanfile.py` and expects to use it across all configurations. According to the [CMake documentation](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS.html), `CMAKE__FLAGS` is supposed to be used for "Language-wide flags for language used when building for all configurations," and "The flags in this variable will be passed before those in the per-configuration `CMAKE__FLAGS_` variable." Furthermore, `CMAKE__FLAGS` is initialized by [`CMAKE__FLAGS_INIT`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_INIT.html).

My question is: How can I achieve the same behavior as before Conan 2.1.0 with CMakeToolchain and Ninja Multi-Config? Is there a way to set `CMAKE_CXX_FLAGS_INIT` or equivalent flags that apply to all configurations in a Ninja Multi-Config CMake project when using Conan?

I have tried using `conan install` instead of `conan build`, and manually configuring CMake afterwards, which seems to work correctly. However, I would like to understand if there is a better solution that allows me to use `conan build` and have the desired flags applied across all configurations.

Any guidance or suggestions would be appreciated.

### Have you read the CONTRIBUTING guide?

- [X] I've read the CONTRIBUTING guide

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.