Reading errors/warnings from the compiler

Part of the project I'm working on has me write errors/warnings to a log file. I understand that this is the compiler's job, but the project requires me do this instead. Is it possible to read the errors/warnings that the compilers give instead?
for example: the compiler runs into an error of some type, I want to take that error description and put it into a file.

I don't see any other way to possible catch all the possible errors a program can give without doing a bunch of try/catch statements.
Last edited on
You better ask your Prof exactly what he wants you to do instead of guessing for yourself. Or maybe ask your friends at least.

Without knowing what you're supposed to do ourselves we can't suggest you anything. You will definitely be mislead and turn in a wrong assignment. So first go and find out what your assignment is. Or at least ask your friends 'how' you're supposed to do this.

Also please don't make multiple threads on the same exact topic.. You could have used the previous one you had made on this same d act topic.
I did but people stopped responding to it. also i figured this was a different question.

unfortunately, after the professor "clarified" things, none of my classmates still understand what's going on. we're all in a discord trying to figure it out but to no avail.
He can't ask you to do something not related to what he has taught you. And we don't know what you've been taught. If you don't know and neither do your friends then you should ask the Prof. He would definitely tell you at least 'how' you should be doing the assignment if not for more hints than even that.

You get what I'm saying right? Until you know exactly what you're supposed to do, nobody can help you.

So first get to know what you're supposed to do in the first place and then you can ask for as much help as you want.
> the compiler runs into an error of some type, I want to take that error description and put it into a file.

Can't you just redirect the compiler output to a file? For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>

int main()
{
    const std::string diag_file_name = "diagnostics.txt" ;
    const std::string cpp_file_name = __FILE__ ;
    const std::string compile_command = "clang++ -std=c++2a -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread -march=native " + 
                                        cpp_file_name + " 2>>" + diag_file_name ; 
    
    std::system( compile_command.c_str() ) ;
    
    std::cout << "\n----------------- compiler diagnostics written to file " << diag_file_name << " ----------------\n"
              << std::ifstream(diag_file_name).rdbuf() 
              << "------------------------------------------- end file ------------------------------------\n" ;
              
    // generate some diagnostics
    int i ; // uninitialised
    int j = i ; // i is uninitialised, j is unused
    i = i++ ; // undefined behaviour
}

http://coliru.stacked-crooked.com/a/49d473e57934724b
@JLBorges

If the compiler gives you error, that means the code doesn't compile.
If the code doesn't compile, how can you expect it to write how it doesn't compile on a file?
I'm curious
H00G0 I'm so damn sure you're going to look at the reply and feel so damn stupid! XD
No worries it happens with everybody ;)
@Grime

Is there something I've missed in the previous answers, or are you just hinting at the fact that JLBorges is going to blow my mind with his answer haha?
Think. Does the compiler fail when it comes across errors or will it try to compile even your English textbook?

If compilers failed when they ran into an error they wouldn't ha e been able to spot multiple errors.

You don't need to be able to run a program to get the list of errors associated with it.
I know the compiler doesn't fail. But if the program itself is supposed to write to a file all that the compiler outputs AND the program doesn't compile because of errors, I know the compiler will still output all the errors but how can the program run and successfully write on a file all those if it doesn't compile in the first place?

Maybe I'm having a major brainfart, but are you getting what I mean?
> If the compiler gives you error, that means the code doesn't compile.
> If the code doesn't compile, how can you expect it to write how it doesn't compile on a file?

The prospective situation is as follows:

Program A compiler: this program is already compiled without errors and is a ready to run binary

Text B the source code: Pure text of the program to be compiled. The code may contain errors.

Program C compiler driver: this is the program that invokes the compiler A and redirects its output to a file. This program too is already compiled without errors and is a ready to run binary.

The confusion may have arisen because in the toy example (used to illustrate redirection of compiler output to a file), both B and C were coalesced into a single source file; in this case, the source code should be compilable, though the compiler would ideally generate some diagnostic messages (warnings).
Oh that makes sense! How would you user the driver C on a source code though? If you #include it, it will be "part" of the code, therefore you can't call anything from that which you're including?

Would you have to somehow link your source code to another program which then will try to compile your code and save errors and warnings on a file?

This sounds quite interesting actually
Last edited on
> How would you user the driver C on a source code though?

See lines 9 and 10 in the example code. The string cpp_file_name which holds the path to the file (containing the source code) to be compiled is passed to the compiler via a command line argument.
@JLBorges

Yes I see, cheers for the clarification!
Oh I didn't see the code snippet my bad. Makes sense.
Topic archived. No new replies allowed.