这个问题类似于这个未解决的问题:调试器无法在调试包含在C++ / CLI DLL中的静态库时步入本机代码
我的设置与上述问题相同。我有一个纯C ++静态库,它链接到一个C ++ / CLI DLL,然后由一个C#可执行文件使用。根据设置,我可以调试C #层,也可以调试C#和C ++ / CLI两层。无论我尝试什么,我都无法调试C ++层。我正在使用Visual Studio 2019。
以下是我尝试过的内容以及结果。对于下面描述的所有情况,我在C ++ / CLI和C ++函数上设置了断点(而不是C#)。
- 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.
2. 对于没有本地调试的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.
- 使用本机调试的C#代码、混合调试的C++和C++/CLI:请参考第一点,它们的行为相同。
- 不使用本机调试的C#代码、使用本机调试的C++代码以及混合调试的C++/CLI:与第二点相同。
我拥有的代码如下:
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 封装器.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++端。非常感谢任何帮助我解决这个问题。
__debugbreak();
。这将使调试器停在那里。你可能会收到来自调试器的投诉,说符号未加载,源代码路径配置不正确,或者类似的问题。一旦解决了阻止源代码调试的特定问题,当你触发__debugbreak()
时,在源代码中设置断点也将开始工作。 - Ben Voigt