Sublime Forum

Display g++ compiler errors directly in the file

#1

I would like to see errors in my code when working with C++. I had an extension called EasyClangComplete that displays the compiling information using clang compiler. But I would like something that uses g++ compiler. Is there anything out there like this?

0 Likes

#2

Sublime can natively display errors inline from code that you build so long as the sublime-build files you’re using to compile the program knows how to capture them and you have the show_errors_line setting turned on. For example, the C++ Single File.sublime-build file that ships with Sublime looks like this by default:

{
	"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\"",
	"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
	"working_dir": "${file_path}",
	"selector": "source.c, source.c++",

	"variants":
	[
		{
			"name": "Run",
			"shell_cmd": "g++ \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
		}
	]
}

The file_regex tells Sublime how to capture the error output, so as long as the show_errors_inline setting is turned on, it should display them for you in that case. The default is for the setting to be enabled:

    // Shows build errors just under the line on which they occur.
    "show_errors_inline": true,

With that build system, I get this output:

The errors in the build panel for that look like this (except for the path line, which is excessively long :smiley:):

/home/tmartin/cpp_hello.cpp: In function ‘int main(int, char**)’:
/home/tmartin/cpp_hello.cpp:7:5: error: expected ‘;’ before ‘return’
     return 0;
     ^
[Finished in 0.2s with exit code 1]
[shell_cmd: g++ "/home/tmartin/cpp_hello.cpp" -o "/home/tmartin/cpp_hello" && "/home/tmartin/cpp_hello"]
[dir: /home/tmartin]

However if you’re mentioning EasyClangComplete, you might also be referring to a Linter error that tells you about problems in your code even before you’ve compiled?

0 Likes

#3

I understand, thank you! I didn’t know it was called a Linter. What plugin would you recommend for doing this for C++ code that displays g++ style error messages?

0 Likes

#4

You can use LSP see the instructions here

0 Likes