Thread: g++ problems

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    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. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    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 :)
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    Actually this is in C++


    Actually the code is in C++. Any suggestions?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222

    Re: Actually this is in C++


    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.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12
    Sorry, let me clarify. I am writting the batch file executer in C++, so I need to write the program in C++.
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    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!
  14. #8
  15. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    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 :
    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.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    12
    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/

IMN logo majestic logo threadwatch logo seochat tools logo