使用 Visual Studio 进行 Clang 4.0 调试

Clang 4.0 debugging using Visual Studio

我有一个项目,我仍在尝试使用 Clang 和 Visual Studio 在 Windows 上设置它。需要注意的是,我从事过几个 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 标志,它们应该输出符号,果然,我得到了一个为 [=16= 生成的 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 文件并查看源路径是否存在?

在此先感谢您的帮助。

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

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

VS2017 Clang debug

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