title: Clang 4.0 debugging using Visual Studio
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code># 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) $&lt; -o $@ 
  
  # Remove the entire list of objects and the executable
  clean:
      rm -f $(EXEC) $(OBJECTS)
  
  rebuild:
      make -B
  </code></pre>
  
  <p>You'll notice that I've included the <code>-g</code> and <code>-O0</code> flags which should output symbols and sure enough, I get a <code>pdb</code> file generated for <code>output.exe</code> (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 <code>pdb</code> not having references to the source. Here is the debug output in Visual Studio:</p>
  
  <pre><code>'output.exe' (Win32): Loaded 'W:\Scratch\Engine\bin\output.exe'. Symbols loaded.
  </code></pre>
  
  <p>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 (<a href="https://clang.llvm.org/docs/MSVCCompatibility.html" rel="nofollow noreferrer">https://clang.llvm.org/docs/MSVCCompatibility.html</a>) mentions how debug info is a work in progress but that I should be able to generate CodeView info using <code>/Z</code> with <code>7</code> or <code>i</code>. I have tried passing both <code>/Zi</code> and <code>/Z7</code> 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 <code>-g</code> flag, I am indeed able to generate <code>pdb</code> files. </p>
  
  <p>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?</p>
  
  <p>Thanks in advance to any help.</p>