Score:-1

how can I detect if g++ compiled without errors

us flag

I have a script file to automatically compile a C++ program, then automatically run the executable.

script

g++ -o bin/program main.cpp 
bin/program

The problem is, sometimes the compiler encounters an error, and the executable is not generated. Therefor, the script will be running an outdated version.

How can I detect if g++ was unable to generate the executable, so that I may prevent the script from running the file?

Romeo Ninov avatar
in flag
Exit code from `g++` execution?
us flag
how can I check the exit code from g++? @RomeoNinov
Romeo Ninov avatar
in flag
See my answer how to exec the program if `g++` finish with no error
Score:1
in flag

You can check the exit code of execution of g++ like this:

g++ -o bin/program main.cpp 
if [ "$?" -eq 0 ]
then bin/program
fi
Martin avatar
kz flag
Ouch. While this works, this is bad coding style - you should do it like that: `if g++ -o bin/program main.cpp; then ... fi`
Romeo Ninov avatar
in flag
@Martin, `if` in my example is perfectly correct usage. Yes, you can add compilation in `if` but think about the case when your line is 100 chars long. Or more :)
Score:0
cm flag

Check if the file exists first:

test -x foo && ./foo
in flag
won't help if it still exists from a previous successful compilation.
cm flag
True if you don't clean it first.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.