The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
g++ problems
Discuss g++ problems in the C Programming forum on Dev Shed. g++ problems C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 24th, 2003, 06:41 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
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
|

April 24th, 2003, 07:36 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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 
|

April 24th, 2003, 09:04 PM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
Actually this is in C++
Actually the code is in C++. Any suggestions?
|

April 25th, 2003, 09:25 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Re: Actually this is in C++
Quote: Originally posted by jimo9
Actually the code is in C++. Any suggestions? |
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
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
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
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 81
Time spent in forums: 4 m 41 sec
Reputation Power: 11
|
|
|
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!
|

April 25th, 2003, 01:39 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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:
WIFEXITED(status)
Evaluates to a non-zero value if status was returned for a child process that terminated normally.
WEXITSTATUS(status)
If the value of WIFEXITED(status) is non-zero, this macro evaluates to the exit code that the child process passed to the _exit(S) or exit(S) function, or the value that the child process returned from main( ). |
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
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
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/
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|