|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
g++ problems
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
|
|
#2
|
||||
|
||||
|
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:
Code:
#!/bin/sh
g++ -o test test.C
if [ "$?" -ne 0 ]
then
echo "Compilation failed!"
exit 1
fi
Note that the spaces around the [ and ] are required. Hope this helps ![]() |
|
#3
|
|||
|
|||
|
Actually this is in C++
Actually the code is in C++. Any suggestions?
|
|
#4
|
||||
|
||||
|
Re: Actually this is in C++
Quote:
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. |
|
#5
|
|||
|
|||
|
Sorry, let me clarify. I am writting the batch file executer in C++, so I need to write the program in C++.
|
|
#6
|
||||
|
||||
|
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. |
|
#7
|
|||
|
|||
|
Thanks!
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!
|
|
#8
|
||||
|
||||
|
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/m.../waitpid.S.html : Quote:
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. |
|
#9
|
|||
|
|||
|
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++.
__________________
Clay Dowling Lazarus Notes Articles and commentary on web development http://www.lazarusid.com/notes/ |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > g++ problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|