I wanted to see if it was possible to debug clang compiled code in Visual Studio (
clang MSVC compatibility page says it should be working). But the .pdb file generated by clang, while it loads in Visual Studio, doesn't allow to place breakpoints or watch values. After a bit of search I found that the -gcodeview flag is necessary to generate compatible debug information. To my understanding,
codeview is the Microsoft format for debug information, and
program database (PDB) is an "API" to access them and other information. If you don't pass -gcodeview I suppose the linker outputs DWARF symbols in the pdb (not sure).
After passing the flag to clang I can debug. But the display and stepping is weird and I don't know if it's intentional or not.
When stepping, part of the line is highlighted in yellow. At first I though it was showing what data was currently used by the current instruction but I'm not sure. Also you often need to step several time to just move one line. Displaying the disassembly on the side, it seems that stepping steps on each instruction.
Did anyone tried to debug clang compiled code in MSVC ? Is there a way to make it work better ? I tried the -Og flags but it didn't change anything and multiplied the compile time by at least 2.
Also I was surprised that clang was slower to compile. I compiled the same code (about 16k lines of C code, not counting system headers) with both compiler, as C and CPP, with and without linking. I tried to have similar flags on both but couldn't find some on clang. Using MSVC 2017 and clang 6. Here are the results.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | == C ==
cl, C, 1.028s
cl Y:\project\main.c -Feproject.exe -Od -MTd -Gm- -nologo -GR- -EHa- -Oi -fp:fast -WX -W4 -wd4201 -wd4505 -wd4996 -wd4100 -wd4189 -wd4204 -Zi -Fm -FC -DWINDOWS -DTARGET_x64 -DCOMPILER_CL -DDEBUG -DLOG -link -INCREMENTAL:NO -opt:ref -subsystem:windows -entry:mainCRTStartup wininet.lib shell32.lib user32.lib OpenGL32.lib GDI32.lib
clang, C, 1.841s
clang Y:\project\main.c -oproject.exe -O0 -g -gcodeview -fno-exceptions -fno-cxx-exceptions -ffast-math -fno-rtti -Werror -fdiagnostics-absolute-paths -DWINDOWS -DTARGET_x64 -DCOMPILER_CLANG -DDEBUG -DLOG --for-linker -INCREMENTAL:NO --for-linker -opt:ref --for-linker -subsystem:windows --for-linker -entry:mainCRTStartup -lwininet.lib -lshell32.lib -luser32.lib -lOpenGL32.lib -lGDI32.lib
== No linking ==
cl, C, no linker, 0.421s
cl Y:\project\main.c -Feproject.exe -Od -c -MTd -Gm- -nologo -GR- -EHa- -Oi -fp:fast -WX -W4 -wd4201 -wd4505 -wd4996 -wd4100 -wd4189 -wd4204 -Zi -Fm -FC -DWINDOWS -DTARGET_x64 -DCOMPILER_CL -DDEBUG -DLOG
clang, C, no linker, 1.450s
clang Y:\project\main.c -oproject.exe -O0 -g -gcodeview -c -fno-exceptions -ffast-math -fno-rtti -Werror -fdiagnostics-absolute-paths -Wno-c++11-compat-deprecated-writable-strings -Wno-writable-strings -DWINDOWS -DTARGET_x64 -DCOMPILER_CLANG -DDEBUG -DLOG
== CPP ==
cl, CPP, 1.373s
cl Y:\project\main.cpp -Feproject.exe -Od -MTd -Gm- -nologo -GR- -EHa- -Oi -fp:fast -WX -W4 -wd4201 -wd4505 -wd4996 -wd4100 -wd4189 -wd4204 -Zi -Fm -FC -DWINDOWS -DTARGET_x64 -DCOMPILER_CL -DDEBUG -DLOG -link -INCREMENTAL:NO -opt:ref -subsystem:windows -entry:mainCRTStartup wininet.lib shell32.lib user32.lib OpenGL32.lib GDI32.lib
clang, CPP, 2.136s
clang Y:\project\main.cpp -oproject.exe -O0 -g -gcodeview -fno-exceptions -fno-cxx-exceptions -ffast-math -fno-rtti -Werror -fdiagnostics-absolute-paths -Wno-c++11-compat-deprecated-writable-strings -Wno-writable-strings -DWINDOWS -DTARGET_x64 -DCOMPILER_CLANG -DDEBUG -DLOG --for-linker -INCREMENTAL:NO --for-linker -opt:ref --for-linker -subsystem:windows --for-linker -entry:mainCRTStartup -lwininet.lib -lshell32.lib -luser32.lib -lOpenGL32.lib -lGDI32.lib
|
A few other questions:
- Is there an equivalent to -MT / -MTd flag for clang ?
- The -E flag makes clang print the pre-processor result to stdin. Is there a way to print it to a file directly without redirecting the console output ?
- Is there a way to limit the number of error that cl outputs (like clang's -ferror-limit= )?