Clang 4.0 debugging using Visual Studio

Viewed 2288

I have a project that I am still trying to setup using Clang and Visual Studio on Windows. As a caveat, I've worked on several c++ projects but they've all been mature projects where I haven't had to be involved in setting up make files or resolving dependencies hence why I want some experience in doing so.

As a clarification, I am not using the LLVM built into visual studio thing. My goal was to have visual studio be a convenience on top of having a project that can be built with make files but not using CMake.

So far, I have a solution with a single nmake project. This nmake project calls a build.bat file which calls into a make file. This make file looks like this:

# 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

You'll notice that I've included the -g and -O0 flags which should output symbols and sure enough, I get a pdb file generated for output.exe (and also all the o files but I can clean that up later). When I go to debug the project in Visual Studio however, it says the symbols for the module are loaded but breakpoints aren't hitting which I think is pointing to the pdb not having references to the source. Here is the debug output in Visual Studio:

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

Posts about Clang from 2016 and earlier mention that it doesn't generate PDB files yet and that thats a work in progress and sure enough the Clang compability site (https://clang.llvm.org/docs/MSVCCompatibility.html) mentions how debug info is a work in progress but that I should be able to generate CodeView info using /Z with 7 or i. I have tried passing both /Zi and /Z7 to clang and to the linker directly but clang complains about them and the linker ignores them with a warning. This documentation claims to be from Clang 6 that is, from what I can tell, not released yet and is experimental. However, using Clang 4.0 with -g flag, I am indeed able to generate pdb files.

Does anyone have any further information on this? Is there anything else I can provide to determine if I have set all this up correctly? Am I just missing a flag that would correctly provide the sources or am I missing a setting in visual studio to pick the sources? I tried setting sources manually in visual studio at the project and solution level to no effect. Should I look at the pdb file with a pdb viewer of some kind and see if the source paths are there?

Thanks in advance to any help.

2 Answers

For the MSVC Version ( https://llvm.org/builds/) it works like in the following picture but to set clang on VS2017 up you need to install Microsoft's Platform Toolset V1.40 first:

VS2017 Clang debug

Project -> Properties -> C/C++ -> Commandline -> Options: "/Z7"

Related