0

I'm trying to make a PowerShell string to compile a program and if it's correctly compiled, I want to open the the program. The problem that I'm trying to solve is to understand how PowerShell understands when the MiniGW compiler with gcc command (for C files) throws a correct result.

Knowing that == TRUE and == FALSE are not accepted in PowerShell I tried an if without any logical operator

if(gcc '.\FileINeedToCompile.c'){
  Write-Host Correct;
} else {
  Write-Host NotCorrect;
}

but I get the NotCorrect result in the case that the program is correctly compiled and also the case it's not compiled.

I've also tried some varietions with the logical operator -eq like

if((gcc '.\FileINeedToCompile.c') -eq $FALSE)

but I get always the result of the else statement.

I searched everywhere and I really don't know how I could make this working. Thanks for help.

3
  • What does gcc return on a successful compilation? Commented Oct 25, 2018 at 19:22
  • What's returned by gcc '.\FileINeedToCompile.c'? You can check the object type with (gcc '.\FileINeedToCompile.c').getType(). I would speculate that it is a string, so try if((gcc '.\FileINeedToCompile.c') -eq "False") Commented Oct 25, 2018 at 19:23
  • I suggest you look into using make instead of trying to roll your own build system. Commented Oct 25, 2018 at 20:14

1 Answer 1

3

You can run the gcc and capture the error code for processing. I didn't test with gcc, but I did test with a small console app and this works assuming a return code of 0 is success.

gcc '.\FileINeedToCompile.c'
if($LASTEXITCODE -eq 0){
  Write-Host Correct;
} else {
  Write-Host NotCorrect;
}

A bit more info on this can be found here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.