CMake & CoreCLR, Visual Studio debugger issue

761 Views Asked by At

Situation

I'm currently working on a small project where I would like to be run .NET 6.0 managed code from my C++ host.

I started following these MSDN docs, which lead me to this sample project.

This sample project, as expected, works fine. But, when I try to convert it to a CMake project, something goes wrong with the Visual Studio debugger.


Issue

The issue I'm having at the moment is that Visual Studio is using the wrong debugger when starting a debug session.

When starting a debug session in Visual Studio, it uses the Desktop CLR (.NET Framework) Managed debugger; but, when loading in the CoreCLR, it expects the 'Managed (CoreCLR)' debugger.

This is the error given by Visual Studio:

A fatal error has occurred and debugging needs to be terminated.
The debugger was configured to use the Desktop CLR (.NET Framework) Managed debugger,
but the target process loaded the CoreCLR (.NET Core) runtime.
To debug this project, configure it to use the 'Managed (CoreCLR)' debugger.

This issue only occurs when running in a debug session inside of Visual Studio. When running without the debugger attached, the program runs without any issue at all.


Question

So now my question is: How can I specify the debugger Visual Studio should use in my CMakeLists.txt file for my project (if it's even possible)?

Or, how can I change the active debugger in Visual Studio when running a CMake project?


CMakeLists.txt

For reference these are my current CMakeLists.txt files for the project

Root project CMakeLists.txt

# File generated by the CMakeHelper vscode extension
cmake_minimum_required(VERSION 3.16.0)
# Project
project("dotnet-scripting" VERSION 1.0.0.0 LANGUAGES C CXX)
# Project version variable
set(DOTNET_SCRIPTING_VERSION "${dotnet-scripting_VERSION}")
# Set C standard
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_C_STANDARD 11)
# Set CXX standard
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_CXX_STANDARD 11)
# Set binary output directory
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/${CMAKE_BUILD_TYPE}")
# Add child projects
add_subdirectory("dotnet-scripting")

Executable project CMakeLists.txt

# File generated by the CMakeHelper vscode extension
cmake_minimum_required(VERSION 3.16.0)
# Project
project("dotnet-scripting" VERSION 1.0.0 LANGUAGES C CXX)
# Project version variable
set(DOTNET_SCRIPTING_VERSION "${dotnet-scripting_VERSION}" PARENT_SCOPE)
# Set C standard
set(CMAKE_C_STANDARD_REQUIRED true)
set(CMAKE_C_STANDARD 11)
# Set CXX standard
set(CMAKE_CXX_STANDARD_REQUIRED true)
set(CMAKE_CXX_STANDARD 17)
# Source files
Set(DOTNET_SCRIPTING_SOURCE_FILES
    "src/main.cpp"
    "src/coreclr_delegates.h"
    "src/hostfxr.h"
    "src/nethost.h"
)
# Set Target
if(WIN32)
    message("Buidling dotnet-scripting on WIN32")
    add_executable("dotnet-scripting" "${DOTNET_SCRIPTING_SOURCE_FILES}")
    # Set binary name
    set_target_properties("dotnet-scripting" PROPERTIES OUTPUT_NAME "dotnet-scripting")
    set_target_properties("dotnet-scripting" PROPERTIES COMMON_LANGUAGE_RUNTIME "")
endif()
# Global include directories
list(APPEND "DOTNET_SCRIPTING_INCLUDE_DIR_LIST" "${PROJECT_SOURCE_DIR}/include")
# Set target include directories
target_include_directories("dotnet-scripting" PUBLIC "${DOTNET_SCRIPTING_INCLUDE_DIR_LIST}")

NOTE The contents of the source files are the same as in the sample project, except that the 'nativehost.cpp' in the sample has been renamed to 'main.cpp'.


Project directory

The project directory is as follows:

Root
|-bin/Debug
|   |- dotnet-scripting.exe
|   |- dotnet-scripting.ilk
|   |- dotnet-scripting.pdb
|   |- DotNetLib.dll
|   |- DotNetLib.runtimeconfig.json
|   |- hostfxr.dll
|-dotnet-scripting
|   |-src
|      |-coreclr_delegates.h
|      |-hostfxr.h
|      |-main.cpp
|      |-nethost.h
|   |-CMakeLists.txt <-- Project/executable
|-CMakeLists.txt <-- Root

0

There are 0 best solutions below