Email: rbamfordz@gmail.com
LinkedIn: https://www.linkedin.com/in/rbamford1/
Github: https://github.com/Bambofy
First, install LLVM from the installer package provided by the LLVM github repository which can be found here. Once llvm is installed if "... python36.dll not found..." is reported when running 'lldb' the fix is to download the python 3.6 file and then place all of its contents in the same folder as 'lldb' executable. Now that LLVM is setup it can be used to compile C programs, but before the compile command can be used we need to know what kind of computer and architecture that our program will run on. Running the 'clang -v' command will return the triple for the computer that ran the command.
PS C:\Users\R> clang -v clang version 16.0. Target: x86_64-pc-windows-msvc Thread model: posix InstalledDir: C:\Program Files\LLVM\binOnce the triple is known for the computer we can insert it into the compiler command that will transform the specified files into an object file for linking. Notice that the command uses the flags '-g' and '-c'. '-g' indicates that the compile should produce debug symbols, and '-c' indicates that the compiler should not link at this time.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/15/2023 3:19 PM 1044 main.c -a---- 4/15/2023 3:20 PM 25586 main.oThe .o file is ready to be linked together with other object files to create the final executable. Again notice the '-g' flag is provided again to the linking stage to ensure that the debug symbols are created.
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/15/2023 8:19 PM 1044 main.c -a---- 4/15/2023 10:21 PM 598016 main.exe -a---- 4/15/2023 10:21 PM 3426216 main.ilk -a---- 4/15/2023 10:21 PM 25586 main.o -a---- 4/15/2023 10:21 PM 8187904 main.pdbThe main.exe is now compatible on the target platform, but in order to debug the executable some more steps are required which involves LLDB. LLDB is the debugger provided with LLVM, and is what we will use to debug our new executable. There is an issue in some Windows computers that stops .PDB files from being used for debugging information, this is problematic because .PDB is the format clang outputs when linking with debug symbols. To let LLDB use .PDB files all that is required is adding "LLDB_USE_NATIVE_PDB_READER=yes" as an environment variable. Debugging the executable is done by running the lldb main.exe command, and then running the add-dsym main.pdb. The add-dsym command tells LLDB that the information for debugging the program is held in the main.PDB file.
PS C:\Users\Documents\Project> lldb main.exe (lldb) target create "main.exe" (lldb) Current executable set to 'main.exe' (x86_64). (lldb) add-dsym main.pdb symbol file 'main.pdb' has been added to 'main.exe' (lldb)
The Windows SDK contains library files and header files that are used to create new windows applications. The SDK can be installed (as of 26/4/2023) from the official windows website. After installing the SDK, there should be a windows kit in the program files directory as shown below.
Directory: C:\Program Files (x86)\Windows Kits Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 9/24/2022 4:45 PM 10 d----- 9/24/2022 4:34 PM 8.1 d----- 1/18/2021 5:33 PM NETFXSDK
Now that the Windows Kit is installed, the path to it's headers and libraries can be used. The libraries are located inside the kit directory at "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\um\x64" and "C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared". The header files are located inside the kit directory at "C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/um" and "C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/ucrt/x64". To compile a program using the libraries and headers it is necessary to add the directories which contain the headers too the compiliation command for e.g.
clang -c -g main.c -o main.o -std=c99 --target=x86_64-pc-windows-msvc -I"C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/um" -I"C:/Program Files (x86)/Windows Kits/10/Include/10.0.19041.0/shared"Notice that the --target flag for clang, this is described in the previous tutorial called Using LLVM on Windows. Additionally notice the '-c' flag which prevents clang from linking. To link the object file which has been created through the compilation above, specify the additional library directories to a command to clang for e.g.
clang -o main.exe main.o -g -std=c99 --target=x86_64-pc-windows-msvc -L"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/um/x64" -L"C:/Program Files (x86)/Windows Kits/10/Lib/10.0.19041.0/ucrt/x64" -lkernel32 -luser32 -lgdi32