从 C# 调试 C++ 静态库

Debugging C++ static lib from C#

这个问题类似于这个未回答的问题:Debugger does not step into native code when debugging a static lib wrapped in a C++/CLI DLL

设置相同。我有一个纯 C++ 静态库,它链接到 C++/CLI DLL,然后由 C# 可执行文件使用。根据设置,我可以调试 C# 层,或同时调试 C# 和 C++/CLI。无论我尝试什么,我都无法调试 C++ 层。我正在使用 Visual Studio 2019.

这是我的尝试和结果。对于下面描述的所有场景,我在 C++/CLI 和 C++ 函数(不是 C#)上设置了断点。

  1. 具有本机调试功能的 C#、具有自动调试功能的 C++ 和 C++/CLI:调试器在 C# 中调用 C++/CLI 函数时停止,但我无法设置它们。 Visual Studio 没有关于 C++/CLI 端断点的消息(它们处于活动状态但无法命中)。在 C++ 方面,它说:
This breakpoint will not currently be hit. Breakpoints in module clr.dll are not alowed. This module contains the implementation of the underlying runtime you are trying to debug.
  1. 没有本机调试的 C#,具有混合调试的 C++ 和 C++/CLI:C++/CLI 中的断点已命中并处于活动状态。 C++ 断点消失,或出现以下消息:
This breakpoint will not currently be hit. No executable code of the debugger's target code is associated with this line. Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the current debugger code type.
  1. 具有本机调试的 C#、具有混合调试的 C++ 和 C++/CLI:参见第一点,行为相同。
  2. 不带本机调试的 C#、带本机调试的 C++ 和带混合调试的 C++/CLI:与第 2 点相同。

我的代码如下:

C++ Native.hpp:

#pragma once
namespace native
{
   class Native
   {
   public:
      Native();

      bool here() const;
   };
}

C++ Native.cpp:

#include "Native.hpp"

#include <iostream>

namespace native
{
   Native::Native()
   {
      std::cout << "Created native entity!" << std::endl; // breakpoint here
   }

   bool Native::here() const
   {
      std::cout << "Native is here!" << std::endl; // breakpoint here
      return true;
   }
}

C++/CLI Wrapper.h:

#pragma once

#include "../cpp/Native.hpp"

using namespace System;

namespace clr
{
    public ref class Wrapper
    {
   public:
      Wrapper() 
      { 
         Console::WriteLine("Building wrapper for native"); // breakpoint here
         mNative = new native::Native(); 
      }
      ~Wrapper() { delete mNative; }
      bool go() 
      { 
         Console::WriteLine("In wrapper to find native..."); // breakpoint here
         return mNative->here(); 
      }

   private:
      native::Native* mNative;

    };
}

和 C#:

using System;
using clr;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Wrapper w = new Wrapper();
            Console.WriteLine("Finding native through wrapper...");
            w.go();
            Console.WriteLine("Waiting...");
            Console.Read();
        }
    }
}

我已经尝试了几乎所有我能想到的方法,但我无法调试 C++ 端。如果能帮我解决这个问题,我将不胜感激。

它可以像 Enable native code debugging 在 Visual Studio 中未启用一样简单。

docs.microsoft.com 的 Tutorial: Debug C# and C++ in the same debugging session 包含成功的 c#+c++ 调试会话的步骤。