compile error for exit(1)

P

Peter

When compiling the following code to study the execv() system call, I get
the compilation error for exit() shown below.

I'm using ssh from home to access my university Unix console. I was
thinking that maybe the firewall or my access privileges may be the problem.

Can someone please explain.

#include <unistd.h>

main()
{
char * const av[]={"ls","-l",(char *)0};

execv("/bin/ls", av);

perror("execv failed");
exit(1);
}

runls2.c: In function âmainâ:
runls2.c:8: error: syntax error before numeric constant
runls2.c:9: warning: incompatible implicit declaration of built-in function
âexitâ
 
B

Barry Schwarz

When compiling the following code to study the execv() system call, I get

I wonder what kind of function execv is that it takes a system command
twice but that is a completely different issue.
the compilation error for exit() shown below.

I'm using ssh from home to access my university Unix console. I was
thinking that maybe the firewall or my access privileges may be the problem.

Where do you come up with these off the wall assumptions.

A hard lesson every new programmer has to learn is that the compiler
is correct a lot more often than you are. No, compilers are not
perfect. But, when they issue a diagnostic, your first reaction
should be "What in my code is causing it to complain?"
Can someone please explain.

#include <unistd.h>

main()
{
char * const av[]={"ls","-l",(char *)0};

The third expression would be more meaningful if written as NULL.

Did you really mean for the elements of av to be constant pointers to
(non-constant) char? Is that the type of argument execv really
expects?
execv("/bin/ls", av);

perror("execv failed");
exit(1);
}

runls2.c: In function âmainâ:
runls2.c:8: error: syntax error before numeric constant
runls2.c:9: warning: incompatible implicit declaration of built-in function
âexitâ
Because you did not include stdlib.h, there is no prototype in scope
for exit. A C89 compiler must assume that exit returns an int. But
your compiler knows that exit returns void. If you lie to the
compiler, expect it to get upset. At least this time it warned you at
compile time rather than generating code that would confuse you at
execution time.

While you are at it, include stdio.h for perror also.


Remove del for email
 
R

Robert Gamble

When compiling the following code to study the execv() system call, I get
the compilation error for exit() shown below.

I'm using ssh from home to access my university Unix console. I was
thinking that maybe the firewall or my access privileges may be the problem.

I seriously doubt it.
Can someone please explain.

#include <unistd.h>

main()

'int main (void)' is better
{
char * const av[]={"ls","-l",(char *)0};

execv("/bin/ls", av);

perror("execv failed");

you need to '#include said:

you need to '#include said:
}

runls2.c: In function âmainâ:
runls2.c:8: error: syntax error before numeric constant
runls2.c:9: warning: incompatible implicit declaration of built-in function
âexitâ

The compiler output does not seem to match the code you provided: you
should have received a warning about the implicit declaration of
perror and the line before the call to exit() (presumably line 8) is
empty so the error doesn't make sense. Make the changes mentioned
above, if you still have an issue make sure you copy and paste the
exact program along with the complete output from the compiler.

Robert Gamble
 
K

Keith Thompson

Barry Schwarz said:
I wonder what kind of function execv is that it takes a system command
twice but that is a completely different issue.

<OT>
The first argument is the file name of the program to be executed.
The second and successive arguments are the initial values for argv
in the newly executed program.
</OT>

Because of this (and other system dependencies), you can't assume that
the value of argv[0] necessarily has anything to do with the actual
name of the program.

[...]
#include <unistd.h>

main()
{
char * const av[]={"ls","-l",(char *)0};

The third expression would be more meaningful if written as NULL.

Did you really mean for the elements of av to be constant pointers to
(non-constant) char? Is that the type of argument execv really
expects?

<OT>Yes.</OT>

[snip]
 
K

Keith Thompson

Peter said:
#include <unistd.h>

main()
{
char * const av[]={"ls","-l",(char *)0};

execv("/bin/ls", av);

perror("execv failed");
exit(1);
}

runls2.c: In function âmainâ:
runls2.c:8: error: syntax error before numeric constant
runls2.c:9: warning: incompatible implicit declaration of built-in function
âexitâ

The line numbers and error messages do not match the code you posted.
There are errors in your code, but I don't see anything that would be
considered a syntax error.

When you post code here, always copy-and-paste the *exact* code that
you actually compiled. We can't possibly guess which errors are in
your original code, and which you introduced when you re-typed it.
 
M

Mark McIntyre

The line numbers and error messages do not match the code you posted.

Do you remember the VMS debugger, which included all your headers in
the line-numbering, so the canonical hello world programme had 2000+
lines....

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

Coos Haak

Op Sun, 15 Jul 2007 22:30:47 +0100 schreef Mark McIntyre:
Do you remember the VMS debugger, which included all your headers in
the line-numbering, so the canonical hello world programme had 2000+
lines....

What about
cc -E hello.c | wc
;-)
 
M

Mark McIntyre

Op Sun, 15 Jul 2007 22:30:47 +0100 schreef Mark McIntyre:


What about
cc -E hello.c | wc

The fun part with VMS was working out which line was the one you
wanted... eg 2241 in the below output from the C debugger on
OpenVMS7.3 (trimmed for linelength and compiled 2 minutes ago... )

$ r test
hello world
%SYSTEM-F-ACCVIO, access violation, reason mask=04
%TRACE-F-TRACEBACK, symbolic stack dump follows
module name routine name line
TEST main 2241
$ type test.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *p;
printf("hello world\n");
strcpy(p, "bar");
return 0;
}



--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top