C++ Line numbers

Sid Kraft 46 Reputation points

How does one get line numbers on visual studio, C++ output? with error reporting

  1. Gade Harika (INFOSYS LIMITED) 2,850 Reputation points Microsoft External Staff

    Hope you're doing well! Please let us know if the issue persists or resolved. If the answer is helpful, please click "Accept Answer" and kindly approve it. If we didn't get any response from your side. we will proceed to close the thread by 18th june

  2. Gade Harika (INFOSYS LIMITED) 2,850 Reputation points Microsoft External Staff

    We haven’t received any further updates or follow-up on this case, so we’ll proceed with closing it at this time. If you require additional assistance or have new information to share, please feel free to raise new thread, we’re happy to support you further needed.  


Sign in to comment

2 answers

  1. Bruce (SqlWork.com) 84,086 Reputation points

    for a running c++ app, the line numbers are in the .pdb file. you can use the debugger, or ship the .pdb with the .exe and use the stacktrace library, which when reporting a runtime error, will read the .pdb file to convert the address pointers to line numbers. if you are using c++ 23 then try:

    #include <iostream>
    #include <stacktrace>
    
    void main()
    {
     std::cout << std::stacktrace::current() << '\n';
    }
    
    
    0 comments No comments

    Sign in to comment
  2. AI answer

    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):

    1. In Visual Studio, go to Tools > Options.
    2. Navigate to Text Editor > C/C++ > General.
    3. In the Settings section, select the Line numbers checkbox.
    4. Select OK.

    Alternatively, to set line numbers for all languages:

    1. Go to Tools > Options.
    2. Expand All Settings > Languages > Defaults > General.
    3. 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:

    AI-generated content may be incorrect. Read our transparency notes for more information.
Sign in to answer

Your answer