VS 2015:当前不会命中断点。没有为该文档加载任何符号

VS 2015: The breakpoint will not currently be hit. No symbols have been loaded for this document

问题

我有一个 C++ (*.cpp) 文件,它是我无法放置活动断点的静态库项目的一部分。当我这样做时,它会给我在 post 的标题中看到的通知。 我可以在同一个项目中的任何其他 C++ 文件中放置断点,它会在调试过程中命中。这个 C++ 文件没有什么特别之处。我使用 CMake 生成项目,我还在可执行项目中引用 class。没有 linking 错误,只是调试器说没有为该源文件加载调试符号。我已经尝试了几种方法来尝试诊断问题所在,但对于我的情况,我没有一个好的答案。

我试过的

  1. 将此文件与同一项目中的其他 *.cpp 文件进行比较:我 cross-check 此文件与我测试验证的其他文件之间的编译器标志能够放置一个有效的断点。没有什么不同。
  2. 重建整个解决方案:如您所料,这没有帮助
  3. 使用 CMake 重新创建整个解决方案:这也没有任何区别。
  4. 在具有 main() 的源文件中包含文件并引用该文件中的 class:这造成了差异。我能够在 class 中放置断点并且它的代码正在执行,而以前,它不是。

有关代码的更多信息

有没有其他人遇到过这样的问题?在这一点上,我很难理解正在发生的事情。任何有助于阐明这里发生的事情的帮助都会很棒!感谢您花时间阅读这个问题。

编辑:我忘了提到这个问题已经被诊断出来,因为我试图诊断为什么角色创建数据包没有被翻译成 RoCreateCharacterSuccess object 当 the character gets successfully created。后来我发现 RoCreateCharacterSuccess class 没有注册到 RoPacketTranslator class。试图打个断点来检查是否调用了构造函数导致了这个问题。


解决方案

感谢@user1,我弄清楚了发生了什么。所以,在我的可执行文件中,where I refer to this class (or an instance of it), I was only accessing RoCreateCharacterSuccess::getCharacter(), which is in the header-file and probably got inline'd by the compiler into the executable. As far as the compiler could see, that was my only interaction with that class. I wasn't invoking its constructor, which was defined in the cpp file。这意味着编译器有机会优化那部分代码(我确实打开了那个选项)。

解决方案:我将我与之交互的唯一方法RoCreateCharacterSuccess::getCharacter()的定义移动到cpp文件中,因此避免了内联和强制编译器到 link 到定义 RoCreateCharacterSuccess class 的那部分静态库,从而触发向翻译器 class 注册 RoCreateCharacterSuccess class 的行为.

首先,我在这里没有看到任何问题。这不是 C++ 编译器或 linker.

的问题

I can put breakpoints in any other C++ file in that same project and it will be hit during debugging. There's nothing special about this C++ file. I use CMake to generate the projects and I also refer to the class in the executable project. There are no linking errors, just that debugger says that no debug symbols have been loaded for that source file.

这里真正发生的事情是,当您的可执行程序在您的静态库中没有看到对模块 - RoCreateCharacterSuccess.cpp 的显式调用时,它不会 link 到模块。

如果程序需要模块中定义的符号,该模块将得到 linked;如果没有,它将被跳过。因此 RoCreateCharacterSuccess.cpp 模块中的任何函数、符号将永远不存在。

RoCreateCharacterSuccess.cpp 被认为是未使用的模块。

由于尚未为模块加载符号,Visual Studio 最终报告不会命中断点。这是显而易见的。


To make this file be able to have active breakpoints, I have to manually register the packet with the translator in the application.

当您在应用程序中使用翻译器模块手动注册数据包时,RoCreateCharacterSuccess.cpp 模块得到 linked 并且断点可用。您能够在该模块中设置断点。


It's like RoCreateCharacterSuccess.cpp is never part of the static library it's supposed to be in. But if I purposefully change the file to introduce a syntax error, the library fails to build.

BTW,任何程序都是如此,任何语法错误都会导致编译失败。我知道您这样做是为了检查您的程序是否正在加载正确的模块,但这确实不是问题所在。

Update @Vite Falcon 很高兴我的回答给了您提示,让您找出断点未激活的原因。显然,是编译器在优化内联函数调用并破坏模块与程序之间的连接。