2

I'm running execl function to compile a program through my code. And I want to check if the compilation went right. so I wrote b = execl("usr/bin/gcc","cc",path,NULL); and later checked if( b==-1). But even though there were errors in the compilations it didnt get into the if statement. Any ideas why? Thank you!

int b=0;
if ((pid1 = fork())<0)
    perror("Error forking");
else {
    if(pid1==0)
        b= execl("/usr/bin/gcc","cc",path,NULL);
    else wait(&status);
}
if(b==-1)
    printf("\n--------\n");
marco polo
  • 159
  • 1
  • 2
  • 12
  • 2
    If any of the functions in the [`exec`](http://man7.org/linux/man-pages/man3/execl.3.html) family succeeds, they *won't* return. They only return in case of failure. – Some programmer dude Dec 28 '14 at 13:35
  • 1
    And if you get errors in the compilation, then there won't be any new executable generated, so you have a contradiction in your question: "I have program which doesn't work as I expect, and it didn't compile". The code won't work as expected *because* you have a compiler error, in fact the code will not run at all since you have no program to run. – Some programmer dude Dec 28 '14 at 13:36
  • But thats the problem- i intential wrote somthing to make error during the compilation and it didnt get to the if statement – marco polo Dec 28 '14 at 13:38
  • 1
    Either there is a compiler error, and you need to fix it, or you get a program that you can run. You can't have both. – Some programmer dude Dec 28 '14 at 13:40
  • 1
    Ah -- Joachim, the compile error occurs *in the called program*. I think it still does not return because the *call itself* did not fail -- the compiler did run and did its thing, so no error there. You need another function to call it; possibly `system`. – Jongware Dec 28 '14 at 13:47
  • Have a look at this question http://stackoverflow.com/q/27590228/694576 – alk Dec 28 '14 at 13:47
  • To the OP: When I say that `exec` will return only on errors, I mean internal errors in the `exec` function or the kernel, for example the program could not be found or something similar, not that the program you start returns with an error code (which you will get through the `wait` function in the parent process). If the `exec` function successfully starts the program, it will not return, as the process is being *replaced* by the new program. – Some programmer dude Dec 28 '14 at 13:55

1 Answers1

5

If exec succeeds it will NEVER return. Succeeding means be able to find and launch the command. If it doesn't succeed it will return -1.

What you need is to extract the exit value of the command from the status used in the wait in the parent process.

You have some macros to determine the status of the run of the child process.

  • WIFEXITED(status) will tell you if the command stopped by a call to exit
  • and then you will be able to get the exit status with WEXITSTATUS(status).

If this status equals to 0 then you will know that the command ran successfully, any other value means that the command wasn't able to do its task normally.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69