To see line numbers in Visual Studio for C++ code and use them with error reporting, enable line numbers in the editor. The compiler and IDE then use these line numbers when reporting errors.
Steps to show line numbers (C++ editor):
- In Visual Studio, go to Tools > Options.
- Navigate to Text Editor > C/C++ > General.
- In the Settings section, select the Line numbers checkbox.
- Select OK.
Alternatively, to set line numbers for all languages:
- Go to Tools > Options.
- Expand All Settings > Languages > Defaults > General.
- Select the Line numbers option to enable global line numbers.
Once enabled, line numbers appear on the left side of the editor. When a build error occurs, Visual Studio shows the file and line number in the Error List and in the build Output window. Selecting an error in the Error List jumps directly to that line. You can also press Ctrl+G and enter a line number to navigate to it.
For C/C++ compilation itself, the compiler uses the current line number and filename when reporting errors. These values can be altered with the #line preprocessor directive, which changes what line and file names appear in error messages and in the predefined macros __LINE__ and __FILE__.
References: