使用 Visual Studio 进行 Clang 4.0 调试

Posted

技术标签:

【中文标题】使用 Visual Studio 进行 Clang 4.0 调试【英文标题】:Clang 4.0 debugging using Visual Studio 【发布时间】:2017-07-23 09:52:25 【问题描述】:

我有一个项目,我仍在尝试在 Windows 上使用 Clang 和 Visual Studio 进行设置。需要注意的是,我参与过几个 c++ 项目,但它们都是成熟的项目,我不必参与设置 make 文件或解决依赖关系,因此我想要一些这样做的经验。

澄清一下,我没有使用 Visual Studio 中内置的 LLVM。我的目标是让 Visual Studio 在拥有一个可以使用 make 文件而不是使用 CMake 构建的项目之上提供便利。

到目前为止,我有一个带有单个 nmake 项目的解决方案。这个 nmake 项目调用了一个 build.bat 文件,该文件调用了一个 make 文件。这个 make 文件如下所示:

# Based on PUXAN tutorial
# http://www.puxan.com/web/howto-write-generic-makefiles/

# Compiler choice
CC = clang++ -g -O0
CC_OBJ_FLAGS = -w -v -c

# Name of our executable and also the main run target
EXEC = ../bin/output.exe

# Here we get every cpp file in the source directory to make a list of source files
SOURCES = $(wildcard ../src/*.cpp)

# Here we have mapped all the cpp files to o files and now have a list of o files
TMP_OBJECTS = $(SOURCES:.cpp=.o)

OBJECTS = $(foreach obj,$(TMP_OBJECTS),$(subst src,obj,$(obj)))

INC = -I../lib/glfw-3.2.1/include
LINK = -L../lib/glfw-3.2.1/lib-vc2015 -lglfw3dll -lglfw3 -lopengl32

# compile list of o files into executable
# NOTE: when make is run without a target, the first target is chosen. This target
# should remain the first at all times
$(EXEC): $(OBJECTS)
    $(CC) $(LINK) $(OBJECTS) -o $(EXEC)

# As each o file becomes a target, compile the associated cpp file into the o file
../obj/%.o: ../src/%.cpp
    $(CC) $(CC_OBJ_FLAGS) $(INC) $< -o $@ 

# Remove the entire list of objects and the executable
clean:
    rm -f $(EXEC) $(OBJECTS)

rebuild:
    make -B

你会注意到我已经包含了应该输出符号的-g-O0 标志,果然,我得到了一个为output.exe 生成的pdb 文件(以及所有的o 文件,但我可以稍后清理)。然而,当我在 Visual Studio 中调试项目时,它说模块的符号已加载,但断点没有命中,我认为这是指向 pdb 没有对源的引用。这是 Visual Studio 中的调试输出:

'output.exe' (Win32): Loaded 'W:\Scratch\Engine\bin\output.exe'. Symbols loaded.

关于 2016 年及更早的 Clang 的帖子提到它还没有生成 PDB 文件,这是一项正在进行的工作,果然 Clang 兼容性站点 (https://clang.llvm.org/docs/MSVCCompatibility.html) 提到了调试信息是如何进行的,但是我应该能够使用/Z7i 生成CodeView 信息。我尝试将/Zi/Z7 直接传递给clang 和链接器,但是clang 抱怨它们并且链接器忽略它们并发出警告。该文档声称来自 Clang 6,也就是说,据我所知,尚未发布并且是实验性的。但是,使用带有-g 标志的Clang 4.0,我确实能够生成pdb 文件。

有人知道这方面的更多信息吗?我还能提供什么来确定我是否正确设置了所有这些吗?我只是错过了一个可以正确提供源的标志,还是我错过了 Visual Studio 中的一个设置来选择源?我尝试在项目和解决方案级别的 Visual Studio 中手动设置源,但没有效果。我是否应该使用某种 pdb 查看器查看 pdb 文件并查看源路径是否存在?

提前感谢任何帮助。

【问题讨论】:

【参考方案1】:

等效于-Z7/-Ziclang 选项称为-gcodeview(除了-g 之外还必须使用)。对于 MSVC 样式的命令行选项,您需要改用 clang-cl compiler driver。

【讨论】:

太棒了!澄清一下,添加带有-g 标志的-gcodeview 是有效的,而仅仅-gcodeview 本身是不够的。我到底是怎么错过这个标志的,为什么文档没有把这个标志和-g 标志放在任何地方。非常感谢您的回答! 顺便说一句,你能告诉我们在哪里可以看到 MSVC 的 clang 等效标志,这就是 clang 内部翻译 clang-cl 标志的方式【参考方案2】:

对于 MSVC 版本 (https://llvm.org/builds/),它的工作原理如下图所示,但要在 VS2017 上设置 clang,您需要先安装 Microsoft 的 Platform Toolset V1.40:

VS2017 Clang debug

项目 -> 属性 -> C/C++ -> 命令行 -> 选项:“/Z7”

【讨论】:

以上是关于使用 Visual Studio 进行 Clang 4.0 调试的主要内容,如果未能解决你的问题,请参考以下文章

如何为 Visual Studio Clang-Format 插件提供 clang 格式文件?

Visual Studio 2019 LLVM clang 标志

使用 clang-cl 在 Visual Studio 2019 中使用 Openmp 4/5

为Visual Studio项目生成clang编译数据库

为啥 clang 生成的二进制文件比 Visual Studio 生成的大

Clang Power Tools VsWhere 无法检测到 Visual Studio 2017