April 24th, 2003, 06:41 PM
I am writing a batch file executer. but I am having trouble getting my program to discern if the program compiles. How do I check? For instance "g++ nonexistantfile" should generate an error, so that I do not try running the program that would have been created had the program compiled. Thanks
April 24th, 2003, 07:36 PM
Assuming that your environment is *NIX, you could check the $? variable that sh provides. This variable contains the return code of the last executed command. You can write a shell script something like this:
Note that the spaces around the [ and ] are required. Hope this helps :)
g++ -o test test.C
if [ "$?" -ne 0 ]
echo "Compilation failed!"
April 24th, 2003, 09:04 PM
Actually this is in C++
Actually the code is in C++. Any suggestions?
April 25th, 2003, 09:25 AM
Re: Actually this is in C++
Scorpion's script would still work regardless of the actual filename given to g++ as the source. Besides, a capital 'C' extension is one of the UNIX conventions for a C++ source file name; I just haven't checked to see if g++ recognizes it.
I did however check the $? returned by g++: it is 0 when successful and non-zero (a 1 as I recall) if there was an error.
April 25th, 2003, 09:33 AM
Sorry, let me clarify. I am writting the batch file executer in C++, so I need to write the program in C++.
April 25th, 2003, 10:09 AM
How then are you invoking g++? I know that system() returns the exit code of the command it runs. I'm not so sure about reading the exit code of a process created through exec.
As you were! I just checked the man page on the wait() function:
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
If status is not null, then it contains the exit code of the child process.
April 25th, 2003, 12:18 PM
I was using execvp but when I was using WIFEEXIT it was not showing anything wrong. But the status is showing me exactly what I need, thanks!
April 25th, 2003, 01:39 PM
You might want to research a bit more deeply into the WIFEXITED macro. In particular, its idea of "terminated normally" might not be the same as yours.
From an online man page at http://docsrv.caldera.com:8507/en/ma...waitpid.S.html :
The other WEXIT* macros listed check for things like termination due to a signal the child received or the child having dumped core. So my interpretation of "terminated normally" would be that the child ran completely, handled any problems encountered on its own, and terminated of its own accord with the exit code set according to what it did or did not encounter.
April 28th, 2003, 10:33 AM
As a challenge from my boss I just finished writing my own shell. If you're not experienced, expect some headaches. You get the joy of a shell with the added fun of a programming language.
Get comfortable with the wait commands as well as the associated macros. Also learn about all the fun you can have with pipes (and trust me, they're fun) and input/output redirection.
Although there's no reason you can't use C++ for it, you're going to find that you're writing mostly in C system calls anyway. Might be worthwhile to chuck the overhead of C++.
Articles and commentary on web development