Skip to content

VS: CSharp 32-bit sets wrong platform flag

When CSharp is used as the language in the project command and there is no other language specified, CMake sets the wrong platform flag (/platform:x64), if the project is generated for 32-bit. In CMakeCSharpInformation.cmake, CMAKE_SIZEOF_VOID_P is the condition to set the platform flag correctly, but CMAKE_SIZEOF_VOID_P is not defined. The Project compiles, however there are warnings displayed for a platform mismatch:

C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2203,5): warning MSB3270: There was a mismatch between the processor architecture of the project being built "x86" and the processor architecture of the reference "E:\custom\build\Debug\custom.exe", "AMD64".
This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of your project through the Configuration Manager so as to align the processor architectures between your project and references, or take a dependency on references with a processor architecture that matches the targeted processor architecture of your project. [E:\custom\build\ALL_BUILD.vcxproj]

The Project compiles and runs on my 64 bit machine, but it won't resolve 32 bit dependencies.

CMake Version 3.23.0
Visual Studio 2019
CSharp compiler version 3.11.0

Command line: cmake -G "Visual Studio 16 2019" -A Win32

Example CMake:

cmake_minimum_required(VERSION 3.8.2 FATAL_ERROR)

project ("custom" CSharp)

add_executable (${PROJECT_NAME}
    Program1.cs
)

target_include_directories (${PROJECT_NAME}
    PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.6"
    VS_DOTNET_REFERENCES
        "Microsoft.CSharp;PresentationCore;System;System.Core;System.Data;System.Drawing;System.Windows.Forms;WindowsBase"
    FOLDER "API/DotNet"
)

Example CSharp:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace foo
{
    class Program
    {
        static void Main(string[] args)
        {
            String projectName = "foo";
            String version = "v1.1.0";

            Console.WriteLine("The API " + projectName + " Sample " + version);
                
            
            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
            Environment.ExitCode = 0;
            return;
        }
    }
}
Edited by Brad King
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information