# How to set WarningLevel for CSharp targets

**URL:** https://discourse.cmake.org/t/how-to-set-warninglevel-for-csharp-targets/7005
**Category:** Usage
**Tags:** lang:csharp
**Created:** [December 2, 2022, 7:28am UTC](https://discourse.cmake.org/t/how-to-set-warninglevel-for-csharp-targets/7005 "2022-12-02T07:28:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Knitschi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/knitschi/32/205_2.png) [@Knitschi](https://discourse.cmake.org/u/Knitschi)
#### Post date: [December 2, 2022, 7:28am UTC](https://discourse.cmake.org/t/how-to-set-warninglevel-for-csharp-targets/7005/1 "2022-12-02T07:28:03Z")

</div>

Hi everybody

I am currently working on transforming our hard checked-in `.csproj` files into CMakeLists files. I ran into the problem that I do not know how to set the warning level in this case.  
This is done in the `.csproj` file with the tag

```auto
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
    ...
    <WarningLevel>1</WarningLevel>
    ...
  </PropertyGroup>

```

I know that I can use the target property `VS_GLOBAL_WarningLevel` to add the `WarningLevel` to the plain `PropertyGroup`

```auto
  <PropertyGroup>
    ...
    <WarningLevel>1</WarningLevel>
    ...
  </PropertyGroup>

```

but this does not affect the value in the configuration specific PropertyGroup which seems to override the value from non config specific PropertyGroup. Is there a way to solve this problem?

I am looking for a `VS_GLOBAL_<CONFIG>_<variable>` target property, to access the config specific section, but I do not think that exists.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [December 2, 2022, 1:14pm UTC](https://discourse.cmake.org/t/how-to-set-warninglevel-for-csharp-targets/7005/2 "2022-12-02T13:14:17Z")

</div>

Target-specific properties are usually derived from the flags for the target and/or the CMake target properties. If you replace `/W1` with `/W3` in the flags (or whatever the C# spelling for these flags happens to be), does that work?

---

<div class="post-metadata">

### Author: ![Knitschi](https://discourse.cmake.org/user_avatar/discourse.cmake.org/knitschi/32/205_2.png) [@Knitschi](https://discourse.cmake.org/u/Knitschi)
#### Post date: [December 7, 2022, 6:35am UTC](https://discourse.cmake.org/t/how-to-set-warninglevel-for-csharp-targets/7005/3 "2022-12-07T06:35:58Z")

</div>

Ok it was my mistake. Adding `/warn:4` to the `CMAKE_CSharp_FLAGS_DEBUG` works.  
I am not quite sure what went wrong because I tried setting the compiler flags. Must have made some trival mistake.

I think the behavior is different to what would be expected from a C++ target.

The following code will set warning level 1 to both targets/projects.  
I think for C++ this would use differnet warning levels for both targets.

```auto
set(CMAKE_CSharp_FLAGS_DEBUG "/warn:4 ..." CACHE STRING "" FORCE)
add_subdirectory(foo) # adds csharp target foo
set(CMAKE_CSharp_FLAGS_DEBUG "/warn:1 ..." CACHE STRING "" FORCE)
add_subdirectory(bar) # adds csharp target bar

```

Thanks for your time.
