I guess you don’t have debugging information enabled when compiling the programs. I get this behaviour when not adding -g
to the compiler’s command line. How do you compile the programs exactly?
Debugger for C
The clangd
LSP uses the .clang-format
file to format, so you should need to do nothing else but configure that file.
I have a build system that I found for C a long time ago now not sure, because I wasn’t sure how to setup up mine. I also build it sometimes during the terminal as well build it using gcc -o Example Example.c and that is it. I do not use any other flags while compiling.
Here is my build system that I use:
{
“target”: “terminus_exec”,
“cancel”: “terminus_cancel_build”,
“auto_close”: false,
“focus”: true,
“timeit”: true,
// “post_window_hooks”: [
// [“carry_file_to_pane”, {“direction”: “down”}]
// ],
“shell_cmd”: “gcc -std=c17 “${file}” -o “${file_path}/${file_base_name}” && “${file_path}/${file_base_name}””,
“file_regex”: “^(…[^:]):([0-9]+):?([0-9]+)?:? (.)$”,
“working_dir”: “${file_path}”,
“selector”: “source.c”,
“variants”:
[
{
"name": "Run",
"shell_cmd": "gcc -std=c17 \"${file}\" -o \"${file_path}/${fileb_base_name}\" && \"${file_path}/${file_base_name}\""
}
]
}
I don’t know how to edit here to make it all appear inside a code like structure so I am sorry if it looks weird but the build works fine I use it all the time, if you have a better one or something or know how to make a better one I would appreciate it as I always use terminus to build this and the output is always fine.
I can use the terminal but I use that only if I need to change certain compiling parameters or if I need to time the program.
You need at least to add a -g
to have debug information, like
gcc -std=c17 -g “${file}” -o “${file_path}/${file_base_name}"
but I would change that to
clang -std=c17 -Wall -pedantic -g -o "${file_path}/${file_base_name}" "${file}"
clang
if you use clang, I would only use gcc
if you have actually installed GCC and want to use that - Apple’s clang has the alias gcc
on MacOS. -Wall -pedantic
adds more warnings, you want all the warnings you can get from the compiler, trust me
Okay I wrote the build system with clang and I get the error when building:
Now I probably messed something up with setting this file path and all I have never done this, plus what is the difference between clang and gcc mainly?
As far as the warnings go, I agree for sure but I had no idea about these extra flag paremeters in compilng and I had no idea you can use this clang instead of gcc. I am oblivious totally to this it seems.
Yes, the "${file}"
is missing before the &&
clang
is the C compiler of LLVM, gcc
is the C compiler of GCC. These are two different compilers, so to be clear which one you are using, you should use it’s “real” name. It (theoretically should not make any difference when you aren’t using compiler extensions (which you can’t when setting -stc=c17 -pedantic-error
).
And all C compilers have loads of options and flags and …
Okay I see, okay I managed to set it up now to use clang build system specifically LLVM , I am going to give it a try now to see if the debugging is maybe working or not and I will let you know.
Interesting how many stuff one learns by having someone like you explain these things, I really appreciate your time and effort in helping me solve this as well as additional info you are providing me which is really useful stuff to know.
Nope it seems that the issue is still the same, still nothing on the debugger .
Also do you have any specific settings that you use or find useful for LSP-clangd while we are at this topic?
Try compiling the file manually in a terminal. First delete the executable (just to be sure) and then compile it:
rm C_Euler_Project/Problem3
clang -std=c17 -Wall -pedantic -g -o C_Euler_Project/Problem3 C_Euler_Project/Problem3.c
When you debug Problem3
now, it should work.
Do you have a breakpoint in the source at a location, where it can “break” on normal execution?
Everything is working fine finally, thank you very much, it seems that the clang building did the trick.
Also regarding the clangd settings in the LSP do you have any specific setings set there that are useful for using C and C++?
Glad to hear! You’re welcome.
That’s my LSP-clangd
settings file:
{
"system_binary": "clangd",
"initializationOptions": {
"clangd.all-scopes-completion": true,
"clangd.background-index": true,
"clangd.clang-tidy": true,
"clangd.function-arg-placeholders": true,
"clangd.header-insertion": "iwyu",
"clangd.enable-config": true,
}
}
This also enables clang-tidy, the static code checker of LLVM, which uses the config file .clang-tidy
. But for now I would not configure that.
I use another static linter, cppcheck
, which can be installed using homebrew install cppcheck
and the https://packagecontrol.io/packages/SublimeLinter package with https://packagecontrol.io/packages/SublimeLinter-cppcheck and this configuration (SublimeLinter):
{
"linters": {
"cppcheck": {
"styles": [
{
"phantom": "{msg}",
}
]
},
"cppcheck++": {
"styles": [
{
"phantom": "{msg}",
}
]
},
}
}
Aha , awesome, well I do not use any other linters just plain LSP. I was using them before I found out about LSP and don’t seem to need them anymore that much.
I am using pure C without Cpp yet so I am not sure if there even are any other linters for C like the one you listed for Cpp you are using.
Also about the formatting something you were mentioning, is there a setting that you can change the formatting, cause the LSP formatting on save seems to format a weird 2 or 2.5 tab indentation and I use always 4 spaces as every tab indentation?
Again thanks for all the info and help
Most of them are for C and C++.
Yes, just add
IndentWidth: 4
UseTab: Never
in the .clang-format
, after the BasedOnStyle:
line.
And you’re welcome, I’m glad I could help.