* Newbie gcc question
@ 1999-09-12 15:32 Wayne Willson
1999-09-12 15:47 ` Tim Prince
` (3 more replies)
0 siblings, 4 replies; 26+ messages in thread
From: Wayne Willson @ 1999-09-12 15:32 UTC (permalink / raw)
To: help-gcc
I'm a sys/network admin trying to learn C (this could be the root of the
problem) with the GNU C compiler.
In a learning C book I just bought it gives this example
--------------------------------------------
#include <stdio.h>
viod main()
{
printf("Hello Universe");
}
I type "gcc -o hello hello.c"
This is what I get "hello.c: In function `main':
hello.c:6: warning: return type of `main' is not `int'"
------------------------------------------------------
When I try this it works:
#include <stdio.h>
int main()
{
printf("Hello Universe");
}
------------------------------------------------
This works as well
#include <stdio.h>
main()
{
printf("Hello Universe");
}
-------------------------------------
Could someone tell me why gcc doesn't work with 'void' but works with 'int'
and ' ' ?
Thanks
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 15:32 Newbie gcc question Wayne Willson
@ 1999-09-12 15:47 ` Tim Prince
1999-09-30 23:56 ` Tim Prince
1999-10-01 0:00 ` Tim Prince
1999-09-12 21:47 ` Chris Gregory
` (2 subsequent siblings)
3 siblings, 2 replies; 26+ messages in thread
From: Tim Prince @ 1999-09-12 15:47 UTC (permalink / raw)
To: help-gcc
>why gcc doesn't work with 'void' but works with 'int'
>and ' ' ?
There must be something in the comp.lang.c FAQ on this; this is one of the
great gulfs between the Microsoft camp and the rest of the C world. Most
environments where C programs run expect main() to return an int status code,
and getting this wrong is just as bad as it would be inside your program. C++
gets even more wrought up about this; C has some historical loopholes where
failure to declare is made to default to int, of which this is one. Speaking
as a Fortran programmer who has to work in environments which expect programs
to run like C, and has a great familiarity with undeclared things.
Tim Prince
tprince@computer.org
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 15:47 ` Tim Prince
@ 1999-09-30 23:56 ` Tim Prince
1999-10-01 0:00 ` Tim Prince
1 sibling, 0 replies; 26+ messages in thread
From: Tim Prince @ 1999-09-30 23:56 UTC (permalink / raw)
To: help-gcc
>why gcc doesn't work with 'void' but works with 'int'
>and ' ' ?
There must be something in the comp.lang.c FAQ on this; this is one of the
great gulfs between the Microsoft camp and the rest of the C world. Most
environments where C programs run expect main() to return an int status code,
and getting this wrong is just as bad as it would be inside your program. C++
gets even more wrought up about this; C has some historical loopholes where
failure to declare is made to default to int, of which this is one. Speaking
as a Fortran programmer who has to work in environments which expect programs
to run like C, and has a great familiarity with undeclared things.
Tim Prince
tprince@computer.org
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 15:47 ` Tim Prince
1999-09-30 23:56 ` Tim Prince
@ 1999-10-01 0:00 ` Tim Prince
1 sibling, 0 replies; 26+ messages in thread
From: Tim Prince @ 1999-10-01 0:00 UTC (permalink / raw)
To: help-gcc
>why gcc doesn't work with 'void' but works with 'int'
>and ' ' ?
There must be something in the comp.lang.c FAQ on this; this is one of the
great gulfs between the Microsoft camp and the rest of the C world. Most
environments where C programs run expect main() to return an int status code,
and getting this wrong is just as bad as it would be inside your program. C++
gets even more wrought up about this; C has some historical loopholes where
failure to declare is made to default to int, of which this is one. Speaking
as a Fortran programmer who has to work in environments which expect programs
to run like C, and has a great familiarity with undeclared things.
Tim Prince
tprince@computer.org
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 15:32 Newbie gcc question Wayne Willson
1999-09-12 15:47 ` Tim Prince
@ 1999-09-12 21:47 ` Chris Gregory
1999-09-30 23:56 ` Chris Gregory
1999-10-01 0:00 ` Chris Gregory
1999-09-30 23:56 ` Wayne Willson
1999-10-01 0:00 ` Wayne Willson
3 siblings, 2 replies; 26+ messages in thread
From: Chris Gregory @ 1999-09-12 21:47 UTC (permalink / raw)
To: help-gcc
On Sun, 12 Sep 1999 17:30:24 -0500, Wayne Willson <wmw_will@yahoo.com> wrote:
>I'm a sys/network admin trying to learn C (this could be the root of the
>problem) with the GNU C compiler.
>
>In a learning C book I just bought it gives this example
>
>--------------------------------------------
>#include <stdio.h>
>
>viod main()
>{
> printf("Hello Universe");
>}
>
>
>
>I type "gcc -o hello hello.c"
>
>This is what I get "hello.c: In function `main':
>hello.c:6: warning: return type of `main' is not `int'"
>------------------------------------------------------
Well, it still worked, you got a warning not an error. Even when I don't need
a return I usually just accept that main is supposed to return an int and just
add a return 0; to the end of the program. It shuts up the annoying warning.
--
Chris Gregory <cmg5@home.com>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 21:47 ` Chris Gregory
@ 1999-09-30 23:56 ` Chris Gregory
1999-10-01 0:00 ` Chris Gregory
1 sibling, 0 replies; 26+ messages in thread
From: Chris Gregory @ 1999-09-30 23:56 UTC (permalink / raw)
To: help-gcc
On Sun, 12 Sep 1999 17:30:24 -0500, Wayne Willson <wmw_will@yahoo.com> wrote:
>I'm a sys/network admin trying to learn C (this could be the root of the
>problem) with the GNU C compiler.
>
>In a learning C book I just bought it gives this example
>
>--------------------------------------------
>#include <stdio.h>
>
>viod main()
>{
> printf("Hello Universe");
>}
>
>
>
>I type "gcc -o hello hello.c"
>
>This is what I get "hello.c: In function `main':
>hello.c:6: warning: return type of `main' is not `int'"
>------------------------------------------------------
Well, it still worked, you got a warning not an error. Even when I don't need
a return I usually just accept that main is supposed to return an int and just
add a return 0; to the end of the program. It shuts up the annoying warning.
--
Chris Gregory <cmg5@home.com>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: Newbie gcc question
1999-09-12 21:47 ` Chris Gregory
1999-09-30 23:56 ` Chris Gregory
@ 1999-10-01 0:00 ` Chris Gregory
1 sibling, 0 replies; 26+ messages in thread
From: Chris Gregory @ 1999-10-01 0:00 UTC (permalink / raw)
To: help-gcc
On Sun, 12 Sep 1999 17:30:24 -0500, Wayne Willson <wmw_will@yahoo.com> wrote:
>I'm a sys/network admin trying to learn C (this could be the root of the
>problem) with the GNU C compiler.
>
>In a learning C book I just bought it gives this example
>
>--------------------------------------------
>#include <stdio.h>
>
>viod main()
>{
> printf("Hello Universe");
>}
>
>
>
>I type "gcc -o hello hello.c"
>
>This is what I get "hello.c: In function `main':
>hello.c:6: warning: return type of `main' is not `int'"
>------------------------------------------------------
Well, it still worked, you got a warning not an error. Even when I don't need
a return I usually just accept that main is supposed to return an int and just
add a return 0; to the end of the program. It shuts up the annoying warning.
--
Chris Gregory <cmg5@home.com>
^ permalink raw reply [flat|nested] 26+ messages in thread
* Newbie gcc question
1999-09-12 15:32 Newbie gcc question Wayne Willson
1999-09-12 15:47 ` Tim Prince
1999-09-12 21:47 ` Chris Gregory
@ 1999-09-30 23:56 ` Wayne Willson
1999-10-01 0:00 ` Wayne Willson
3 siblings, 0 replies; 26+ messages in thread
From: Wayne Willson @ 1999-09-30 23:56 UTC (permalink / raw)
To: help-gcc
I'm a sys/network admin trying to learn C (this could be the root of the
problem) with the GNU C compiler.
In a learning C book I just bought it gives this example
--------------------------------------------
#include <stdio.h>
viod main()
{
printf("Hello Universe");
}
I type "gcc -o hello hello.c"
This is what I get "hello.c: In function `main':
hello.c:6: warning: return type of `main' is not `int'"
------------------------------------------------------
When I try this it works:
#include <stdio.h>
int main()
{
printf("Hello Universe");
}
------------------------------------------------
This works as well
#include <stdio.h>
main()
{
printf("Hello Universe");
}
-------------------------------------
Could someone tell me why gcc doesn't work with 'void' but works with 'int'
and ' ' ?
Thanks
^ permalink raw reply [flat|nested] 26+ messages in thread
* Newbie gcc question
1999-09-12 15:32 Newbie gcc question Wayne Willson
` (2 preceding siblings ...)
1999-09-30 23:56 ` Wayne Willson
@ 1999-10-01 0:00 ` Wayne Willson
3 siblings, 0 replies; 26+ messages in thread
From: Wayne Willson @ 1999-10-01 0:00 UTC (permalink / raw)
To: help-gcc
I'm a sys/network admin trying to learn C (this could be the root of the
problem) with the GNU C compiler.
In a learning C book I just bought it gives this example
--------------------------------------------
#include <stdio.h>
viod main()
{
printf("Hello Universe");
}
I type "gcc -o hello hello.c"
This is what I get "hello.c: In function `main':
hello.c:6: warning: return type of `main' is not `int'"
------------------------------------------------------
When I try this it works:
#include <stdio.h>
int main()
{
printf("Hello Universe");
}
------------------------------------------------
This works as well
#include <stdio.h>
main()
{
printf("Hello Universe");
}
-------------------------------------
Could someone tell me why gcc doesn't work with 'void' but works with 'int'
and ' ' ?
Thanks
^ permalink raw reply [flat|nested] 26+ messages in thread
* newbie gcc question
@ 1999-10-13 15:54 Maggie Owens
1999-10-13 16:09 ` Matthew Majka
` (3 more replies)
0 siblings, 4 replies; 26+ messages in thread
From: Maggie Owens @ 1999-10-13 15:54 UTC (permalink / raw)
To: help-gcc
I have a little experience writing C++ in the dos and windows
environment. Now I am trying to write a c program and compile it in gcc
on a unix system. As a test, I wrote the traditional hello, world
program:
#include <stdio.h>
void main()
{
printf("Hello, world!");
}
I compiled it using:
gcc helloworld.c
Now, I have a.out, which appears to be executable.
I am using the z shell.
I tried typing:
a.out
and got:
zsh: command not found: a.out
so I tried
sh a.out
and got
a.out: (M-^A)^C^A^K: not found
a.out: syntax error at line 10: `;;' unexpected
finally, I tried
./a.out
and got nothing in response
I feel abysmally stupid. How can I execute my program?
Thanks in advance
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 15:54 newbie " Maggie Owens
@ 1999-10-13 16:09 ` Matthew Majka
1999-10-14 0:05 ` Johan Nilson
1999-10-31 13:57 ` Matthew Majka
1999-10-13 22:53 ` Konstantinos Adamopoulos
` (2 subsequent siblings)
3 siblings, 2 replies; 26+ messages in thread
From: Matthew Majka @ 1999-10-13 16:09 UTC (permalink / raw)
To: help-gcc
Maggie Owens wrote:
>
> I have a little experience writing C++ in the dos and windows
> environment. Now I am trying to write a c program and compile it in gcc
> on a unix system. As a test, I wrote the traditional hello, world
> program:
> #include <stdio.h>
> void main()
> {
> printf("Hello, world!");
> }
>
> I compiled it using:
> gcc helloworld.c
gcc helloworld.c -o helloworld
./helloworld
That should do it. However, ./a,out should have done it also.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 16:09 ` Matthew Majka
@ 1999-10-14 0:05 ` Johan Nilson
1999-10-14 9:58 ` Matthew Majka
1999-10-31 13:57 ` Johan Nilson
1999-10-31 13:57 ` Matthew Majka
1 sibling, 2 replies; 26+ messages in thread
From: Johan Nilson @ 1999-10-14 0:05 UTC (permalink / raw)
To: help-gcc
On Wed, 13 Oct 1999 17:05:41 -0600, Matthew Majka <mmajka@cs5.dasd.honeywell.com> wrote:
>Maggie Owens wrote:
>>
>> I have a little experience writing C++ in the dos and windows
>> environment. Now I am trying to write a c program and compile it in gcc
>> on a unix system. As a test, I wrote the traditional hello, world
>> program:
>> #include <stdio.h>
>> void main()
>> {
>> printf("Hello, world!");
>> }
>>
>> I compiled it using:
>> gcc helloworld.c
>
>gcc helloworld.c -o helloworld
>./helloworld
>
>That should do it. However, ./a,out should have done it also.
or simply renaming a.out to a.exe, and then running it
/Johan
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-14 0:05 ` Johan Nilson
@ 1999-10-14 9:58 ` Matthew Majka
1999-10-31 13:57 ` Matthew Majka
1999-10-31 13:57 ` Johan Nilson
1 sibling, 1 reply; 26+ messages in thread
From: Matthew Majka @ 1999-10-14 9:58 UTC (permalink / raw)
To: help-gcc
Johan Nilson wrote:
>
> On Wed, 13 Oct 1999 17:05:41 -0600, Matthew Majka <mmajka@cs5.dasd.honeywell.com> wrote:
>
> >Maggie Owens wrote:
> >>
> >> I have a little experience writing C++ in the dos and windows
> >> environment. Now I am trying to write a c program and compile it in gcc
> >> on a unix system. As a test, I wrote the traditional hello, world
> >> program:
> >> #include <stdio.h>
> >> void main()
> >> {
> >> printf("Hello, world!");
> >> }
> >>
> >> I compiled it using:
> >> gcc helloworld.c
> >
> >gcc helloworld.c -o helloworld
> >./helloworld
> >
> >That should do it. However, ./a,out should have done it also.
>
> or simply renaming a.out to a.exe, and then running it
He's on UNIX, not Windoze.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-14 9:58 ` Matthew Majka
@ 1999-10-31 13:57 ` Matthew Majka
0 siblings, 0 replies; 26+ messages in thread
From: Matthew Majka @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
Johan Nilson wrote:
>
> On Wed, 13 Oct 1999 17:05:41 -0600, Matthew Majka <mmajka@cs5.dasd.honeywell.com> wrote:
>
> >Maggie Owens wrote:
> >>
> >> I have a little experience writing C++ in the dos and windows
> >> environment. Now I am trying to write a c program and compile it in gcc
> >> on a unix system. As a test, I wrote the traditional hello, world
> >> program:
> >> #include <stdio.h>
> >> void main()
> >> {
> >> printf("Hello, world!");
> >> }
> >>
> >> I compiled it using:
> >> gcc helloworld.c
> >
> >gcc helloworld.c -o helloworld
> >./helloworld
> >
> >That should do it. However, ./a,out should have done it also.
>
> or simply renaming a.out to a.exe, and then running it
He's on UNIX, not Windoze.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-14 0:05 ` Johan Nilson
1999-10-14 9:58 ` Matthew Majka
@ 1999-10-31 13:57 ` Johan Nilson
1 sibling, 0 replies; 26+ messages in thread
From: Johan Nilson @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
On Wed, 13 Oct 1999 17:05:41 -0600, Matthew Majka <mmajka@cs5.dasd.honeywell.com> wrote:
>Maggie Owens wrote:
>>
>> I have a little experience writing C++ in the dos and windows
>> environment. Now I am trying to write a c program and compile it in gcc
>> on a unix system. As a test, I wrote the traditional hello, world
>> program:
>> #include <stdio.h>
>> void main()
>> {
>> printf("Hello, world!");
>> }
>>
>> I compiled it using:
>> gcc helloworld.c
>
>gcc helloworld.c -o helloworld
>./helloworld
>
>That should do it. However, ./a,out should have done it also.
or simply renaming a.out to a.exe, and then running it
/Johan
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 16:09 ` Matthew Majka
1999-10-14 0:05 ` Johan Nilson
@ 1999-10-31 13:57 ` Matthew Majka
1 sibling, 0 replies; 26+ messages in thread
From: Matthew Majka @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
Maggie Owens wrote:
>
> I have a little experience writing C++ in the dos and windows
> environment. Now I am trying to write a c program and compile it in gcc
> on a unix system. As a test, I wrote the traditional hello, world
> program:
> #include <stdio.h>
> void main()
> {
> printf("Hello, world!");
> }
>
> I compiled it using:
> gcc helloworld.c
gcc helloworld.c -o helloworld
./helloworld
That should do it. However, ./a,out should have done it also.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 15:54 newbie " Maggie Owens
1999-10-13 16:09 ` Matthew Majka
@ 1999-10-13 22:53 ` Konstantinos Adamopoulos
1999-10-31 13:57 ` Konstantinos Adamopoulos
1999-10-14 12:25 ` Maggie Owens
1999-10-31 13:57 ` Maggie Owens
3 siblings, 1 reply; 26+ messages in thread
From: Konstantinos Adamopoulos @ 1999-10-13 22:53 UTC (permalink / raw)
To: help-gcc
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3879 bytes --]
I don't know what kind of system you are using (unix, linux or what),
but anyway here are some suggestions to try:
1. I have never tried Z shell myself. Maybe you should switch to
a different shell by using one of the following commands and then try to
execute your program again:
   csh      Â
-- to run c shell
   bash      --
to run bash shell
   ksh      Â
-- to run korn shell
2. If the current directory is not in your path you should always
use ./filename to execute your binary files, for example
   ./a.out
3. Check if the output file (a.out) is executable. Use the command
   ls -l
and you should have a line like the following one for your a.out
file
-rwx------Â Â 1 kadamopo 1UGCS4Â Â Â Â Â
6776 Oct 14 06:13 a.out
The important bit is the x (4th characters), which means that your
file is executable. If is not there give the command:
chmod 700 a.out
in order to make your file executable
4. Maybe your compiler is confused with the extension of your file
name, for example .c mean c program, and usually .C or .cpp means c++ program.
In a c program usually you can't use expressions like
void main()
because this is specific for c++ programs and the c compiler is
expecting a returned value. It is better to use something like
 #include <stdio.h>
int main()
{
   printf("Hello, world!");
       return 0;
}
or to rename your file to something like helloworld.cpp or helloworld.C
and try it with its original contents without to change anything.
------------------------------------------------------------------------------------------------------
To conclud:
Your program seems to work fine in my system (running Sun OS and gcc-2.95)
as it is
 #include <stdio.h>
 void main()
 {
   printf("Hello, world!");
 }
when I save it as helloworld.cpp and compile with
gcc helloworld.cpp (then run ./a.out)
or
gcc helloworld.cpp -o helloworld (then run ./helloworld)
When I save it as helloworld.c needs the modification I mentioned before,
and looks like this:
#include <stdio.h>
int main()
{
   printf("Hello, world!");
       return 0;
}
compile with
gcc helloworld.c (then run ./a.out)
or
gcc helloworld.c -o helloworld (then run ./helloworld)
------------------------------------------------------------------------------------------------------
Hope that's a bit of help,
Kostas
Â
Maggie Owens wrote:
I have a little experience writing C++ in the dos
and windows
environment. Now I am trying to write a c program and compile it in
gcc
on a unix system. As a test, I wrote the traditional hello, world
program:
 #include <stdio.h>
 void main()
 {
   printf("Hello, world!");
 }
I compiled it using:
 gcc helloworld.c
Now, I have a.out, which appears to be executable.
I am using the z shell.
I tried typing:
  a.out
and got:
  zsh: command not found: a.out
so I tried
  sh a.out
and got
  a.out: (M-^A)^C^A^K: not found
  a.out: syntax error at line 10: `;;' unexpected
finally, I tried
  ./a.out
and got nothing in response
I feel abysmally stupid. How can I execute my program?
Thanks in advance
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
--
====================================================
Konstantinos Adamopoulos
Final Year Student in Computer Science
Department of Computing - University of Bradford
----------------------------------------------------
Phone: +44 1274 238732
Fax : +1 909 257 7791
email: kadamopo@comp.brad.ac.uk
WWWÂ : http://www.student.comp.brad.ac.uk/~kadamopo
====================================================
Â
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 22:53 ` Konstantinos Adamopoulos
@ 1999-10-31 13:57 ` Konstantinos Adamopoulos
0 siblings, 0 replies; 26+ messages in thread
From: Konstantinos Adamopoulos @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3879 bytes --]
I don't know what kind of system you are using (unix, linux or what),
but anyway here are some suggestions to try:
1. I have never tried Z shell myself. Maybe you should switch to
a different shell by using one of the following commands and then try to
execute your program again:
   csh      Â
-- to run c shell
   bash      --
to run bash shell
   ksh      Â
-- to run korn shell
2. If the current directory is not in your path you should always
use ./filename to execute your binary files, for example
   ./a.out
3. Check if the output file (a.out) is executable. Use the command
   ls -l
and you should have a line like the following one for your a.out
file
-rwx------Â Â 1 kadamopo 1UGCS4Â Â Â Â Â
6776 Oct 14 06:13 a.out
The important bit is the x (4th characters), which means that your
file is executable. If is not there give the command:
chmod 700 a.out
in order to make your file executable
4. Maybe your compiler is confused with the extension of your file
name, for example .c mean c program, and usually .C or .cpp means c++ program.
In a c program usually you can't use expressions like
void main()
because this is specific for c++ programs and the c compiler is
expecting a returned value. It is better to use something like
 #include <stdio.h>
int main()
{
   printf("Hello, world!");
       return 0;
}
or to rename your file to something like helloworld.cpp or helloworld.C
and try it with its original contents without to change anything.
------------------------------------------------------------------------------------------------------
To conclud:
Your program seems to work fine in my system (running Sun OS and gcc-2.95)
as it is
 #include <stdio.h>
 void main()
 {
   printf("Hello, world!");
 }
when I save it as helloworld.cpp and compile with
gcc helloworld.cpp (then run ./a.out)
or
gcc helloworld.cpp -o helloworld (then run ./helloworld)
When I save it as helloworld.c needs the modification I mentioned before,
and looks like this:
#include <stdio.h>
int main()
{
   printf("Hello, world!");
       return 0;
}
compile with
gcc helloworld.c (then run ./a.out)
or
gcc helloworld.c -o helloworld (then run ./helloworld)
------------------------------------------------------------------------------------------------------
Hope that's a bit of help,
Kostas
Â
Maggie Owens wrote:
I have a little experience writing C++ in the dos
and windows
environment. Now I am trying to write a c program and compile it in
gcc
on a unix system. As a test, I wrote the traditional hello, world
program:
 #include <stdio.h>
 void main()
 {
   printf("Hello, world!");
 }
I compiled it using:
 gcc helloworld.c
Now, I have a.out, which appears to be executable.
I am using the z shell.
I tried typing:
  a.out
and got:
  zsh: command not found: a.out
so I tried
  sh a.out
and got
  a.out: (M-^A)^C^A^K: not found
  a.out: syntax error at line 10: `;;' unexpected
finally, I tried
  ./a.out
and got nothing in response
I feel abysmally stupid. How can I execute my program?
Thanks in advance
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
--
====================================================
Konstantinos Adamopoulos
Final Year Student in Computer Science
Department of Computing - University of Bradford
----------------------------------------------------
Phone: +44 1274 238732
Fax : +1 909 257 7791
email: kadamopo@comp.brad.ac.uk
WWWÂ : http://www.student.comp.brad.ac.uk/~kadamopo
====================================================
Â
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-13 15:54 newbie " Maggie Owens
1999-10-13 16:09 ` Matthew Majka
1999-10-13 22:53 ` Konstantinos Adamopoulos
@ 1999-10-14 12:25 ` Maggie Owens
1999-10-31 13:57 ` Maggie Owens
1999-10-31 13:57 ` Maggie Owens
3 siblings, 1 reply; 26+ messages in thread
From: Maggie Owens @ 1999-10-14 12:25 UTC (permalink / raw)
To: help-gcc
Thank you all for your kind help and detailed instructions!
The solution to the problem was simple, and I suppose it might have
something to do with my terminal type:
I had to add a \n to the end of the printf statement, so it now reads:
printf("Hello, world!\n");
Now it works as expected when I type
./a.out
Cool.
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question
1999-10-14 12:25 ` Maggie Owens
@ 1999-10-31 13:57 ` Maggie Owens
0 siblings, 0 replies; 26+ messages in thread
From: Maggie Owens @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
Thank you all for your kind help and detailed instructions!
The solution to the problem was simple, and I suppose it might have
something to do with my terminal type:
I had to add a \n to the end of the printf statement, so it now reads:
printf("Hello, world!\n");
Now it works as expected when I type
./a.out
Cool.
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 26+ messages in thread
* newbie gcc question
1999-10-13 15:54 newbie " Maggie Owens
` (2 preceding siblings ...)
1999-10-14 12:25 ` Maggie Owens
@ 1999-10-31 13:57 ` Maggie Owens
3 siblings, 0 replies; 26+ messages in thread
From: Maggie Owens @ 1999-10-31 13:57 UTC (permalink / raw)
To: help-gcc
I have a little experience writing C++ in the dos and windows
environment. Now I am trying to write a c program and compile it in gcc
on a unix system. As a test, I wrote the traditional hello, world
program:
#include <stdio.h>
void main()
{
printf("Hello, world!");
}
I compiled it using:
gcc helloworld.c
Now, I have a.out, which appears to be executable.
I am using the z shell.
I tried typing:
a.out
and got:
zsh: command not found: a.out
so I tried
sh a.out
and got
a.out: (M-^A)^C^A^K: not found
a.out: syntax error at line 10: `;;' unexpected
finally, I tried
./a.out
and got nothing in response
I feel abysmally stupid. How can I execute my program?
Thanks in advance
--
Autoreplies will (hopefully) bounce. To reply to me, please send mail
to mmowens at panix dot com.
Sent via Deja.com http://www.deja.com/
Before you buy.
^ permalink raw reply [flat|nested] 26+ messages in thread
* newbie gcc question...
@ 2002-04-29 18:21 Michael Kahle
2002-04-29 18:45 ` Claudio Bley
2002-04-29 19:48 ` Andrea 'Fyre Wyzard' Bocci
0 siblings, 2 replies; 26+ messages in thread
From: Michael Kahle @ 2002-04-29 18:21 UTC (permalink / raw)
To: gcc-help
hello. I hope this question is not creating any unnecessary noise.
If it is, i apologize up front and ask you lead me to the correct faq
or other helpful document.
I am trying to teach myself the c language and a program I am creating
at compile time is giving me the error:
undefined reference to 'bla_bla1'
undefined reference to 'bla_bla2'
undefined reference to 'bla_bla3'
etc...
This is the first c program I have written that requires me to use the
#include directive in my program.
I have a file called
bla_bla.h
in this file i have the lines:
void bla_bla1(int);
int bla_bla2(int);
int bla_bla3(int);
I also have another file called
tbla_bla.c
that has the directive:
#include "bla_bla.h"
in it. When I try and compile this I get the error above. The files
are all located in the same directory and I run gcc from that directory
with the following command line...
gcc -d -l bla_bla.h bla_bla.c -o bla_bla.o
What is causing this error?! I know I am doing something wrong. Unfortunatly
the O'Reilly book I am using for my learning (Practical C Programming)
doesn't give a hint as to what command line options I should be using
when using gcc to include header files. I have poked around on the net
for my answer and tried quite a few different things.
Please help. I am new to all of this so please be nice. :)
Thank you very much.
Michael
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question...
2002-04-29 18:21 Michael Kahle
@ 2002-04-29 18:45 ` Claudio Bley
2002-04-29 19:48 ` Andrea 'Fyre Wyzard' Bocci
1 sibling, 0 replies; 26+ messages in thread
From: Claudio Bley @ 2002-04-29 18:45 UTC (permalink / raw)
To: Michael Kahle; +Cc: gcc-help
>>>>> "Michael" == Michael Kahle <geopoliticus@onebox.com> writes:
Michael> I am trying to teach myself the c language and a program
Michael> I am creating at compile time is giving me the error:
Michael> undefined reference to 'bla_bla1' undefined reference to
Michael> 'bla_bla2' undefined reference to 'bla_bla3' etc...
Michael> This is the first c program I have written that requires
Michael> me to use the #include directive in my program. I have a
Michael> file called bla_bla.h in this file i have the lines: void
Michael> bla_bla1(int); int bla_bla2(int); int bla_bla3(int); I
Michael> also have another file called tbla_bla.c that has the
Michael> directive: #include "bla_bla.h" in it.
Producing an executable program is usually a 2 step process. At first
you let the compiler generate object files, afterwards the linker
takes these files and links them together to one executable program.
So, to generate object files run
gcc -c <file.c>
on every C source file you've got.
Then, run for example:
gcc foo.o bar.o -lm
This will produce the file a.out (use -o <outfile> to name it
differently) by linking the 2 object files foo.o and bar.o together
and also linking with the math library when necessary (-lm means to
search for libm.so or libm.a).
You should have a look at "Advanced Linux Programming". It gets really
'advanced' on some subjects, but it starts out with the basics:
http://www.advancedlinuxprogramming.com/
HTH
Claudio
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question...
2002-04-29 18:21 Michael Kahle
2002-04-29 18:45 ` Claudio Bley
@ 2002-04-29 19:48 ` Andrea 'Fyre Wyzard' Bocci
1 sibling, 0 replies; 26+ messages in thread
From: Andrea 'Fyre Wyzard' Bocci @ 2002-04-29 19:48 UTC (permalink / raw)
To: Michael Kahle, gcc-help
Hi Michael
When you include a file using the #include directive, you don't need to
include it again from the command line.
The you are getting are generated at link time, when gcc tries to determine
where all the (external) functions and variables you referenced in your
code are.
A very fast description of what's goin'on:
In C (and C++ and many other languages) you have to make a distinction
between the DECLARATION of and function (or a variable) and its DEFINITION.
The declaration of a function tells the compiler that, somewhere, there is
a function with that name, parameters and type. You need only specify the
types, the names of the arguments are optional.
The definition tells what the function actually does. You must put there
the names for the parameters, as you will be using them in the function.
Sometimes declaration and definition are referred to as, respectively,
interface and implementation, because the former tells how a function is
invoked (how your code will interface with it), and the latter actually
implements the function.
Example:
void bla_bla1(int);
is a declaration. It tells that there is a function called bla_bla1(),
taking an int as an argument and returning no value (void).
void bla_bla1(int x) {
printf ("Blah blah blah... %d\n", x);
}
is a definition. It implements bla_bla1() as a function that prints Blah
blah blah...* to the standard output.
To use a function in your code, it must have been previously declared. (the
definition itself counts as a declaration, too.)
That's why, for example, to use the library function printf(), you must
#include <stdio.h>, the header files that contains the declaration of printf().
When you make a program, all the functions you use must (also that being
declared) be defined in exactly 1 place, where all the declarations will be
"pointed at".
Compiling a program is actually made of 2 steps (even if a compiler can
mask this, doing all the work on a single invocation).
[actually, more that two, but you can look at it in this way as a starting
point]
During the 1st step, the compiler proper translates all the source files
(.c, for example) into the respective binary files (.o, or .obj).
During the 2ns step, the linker is invoked, and it merges all the .o files
and all the specified libraries into a single executable, taking care to
resolve all the external object; i.e. all the function that are declared
but not defined are "addressed" to the place where that function is
defined. This place can be a standard library, linked in automatically (for
functions like printf() or malloc()), an other library (for example, math
functions declared in <math.h> are defined in the libm library, linked in
with the "-lm" option), or in another (.o) file.
If you declare and use a function, but never define it (because you have
not written the implementation, or not included the .o file or the
library), you will get undefined references to those functions.
In you example, you declared bla_bla1(), bla_bla2(), bla_bla3(), etc... but
you did not define them.
A correct example would be in the form of:
/* bla_bla.h */
void bla_bla1(int);
int bla_bla2(int);
/* end of bla_bla.h */
/* bla_bla.c */
#include "bla_bla.h"
void bla_bla1 (int x) {
printf ("Blah blah blah... %d\n", x);
}
int bla_bla2 (int y) {
int z = y*y;
printf ("Blah blah blah... %d\n", z);
return z;
}
/* end of bla_bla.c */
/* main.c */
#include "bla_bla.h"
int main (int argc, char** argv) {
bla_bla1 (12);
bla_bla2 (5);
return 0;
}
/* end of bla_bla.c */
You can compile and link this all in one step with
gcc main.c bla_bla.c -o bla
which will build an executable name bla ("-o" is used to choose the name of
the output) , which should print
Blah blah blah... 12
Blah blah blah... 25
If, on the other hand, you want to compile it in two steps, you could do:
gcc main.c -o main.o -c
gcc bla_bla.c -o bla_bla.o -c
The "-c" option instruct the compiler to just compile the code, without
linking.
Then
gcc main.o bla_bla.o -o bla
will build the same executable as before :-)
I hope to have been clear enough. If you need more answers, feel free to
write me.
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question...
@ 2002-04-29 19:51 Michael Kahle
0 siblings, 0 replies; 26+ messages in thread
From: Michael Kahle @ 2002-04-29 19:51 UTC (permalink / raw)
To: Andrea 'Fyre Wyzard' Bocci; +Cc: gcc-help
Andrea,
Thank you so much for the wonderful example and more importantly the
explanation! This silly (now I think so!) problem has been plaguing
me for the past 3 days... don't worry, I took many breaks!
I better understand now what is going on and was able to compile and
run my program!
> In you example, you declared bla_bla1(), bla_bla2(), bla_bla3(), etc...
> but
> you did not define them.
oops! in my program I actually did have the definition in the header
file, I didn't make that clear in my example to the list.
Thanks again. This was such a huge help to me...
Happy programming!
Michael
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: newbie gcc question...
@ 2002-04-29 20:14 Michael Kahle
0 siblings, 0 replies; 26+ messages in thread
From: Michael Kahle @ 2002-04-29 20:14 UTC (permalink / raw)
To: Claudio Bley; +Cc: gcc-help
Claudio,
Thanks for your help! I just finished downloading the pdf book "Advanced
Linux Programming". I am going to read some tonight. Thank you very
much.
Michael
---- "Claudio Bley" <bley@cs.uni-magdeburg.de> wrote:
> >>>>> "Michael" == Michael Kahle <geopoliticus@onebox.com> writes:
>
> Michael> I am trying to teach myself the c language and a program
> Michael> I am creating at compile time is giving me the error:
>
> Michael> undefined reference to 'bla_bla1' undefined reference
> to
> Michael> 'bla_bla2' undefined reference to 'bla_bla3' etc...
>
> Michael> This is the first c program I have written that requires
> Michael> me to use the #include directive in my program. I have
> a
> Michael> file called bla_bla.h in this file i have the lines: void
> Michael> bla_bla1(int); int bla_bla2(int); int bla_bla3(int); I
> Michael> also have another file called tbla_bla.c that has the
> Michael> directive: #include "bla_bla.h" in it.
>
> Producing an executable program is usually a 2 step process. At first
> you let the compiler generate object files, afterwards the linker
> takes these files and links them together to one executable program.
>
> So, to generate object files run
>
> gcc -c <file.c>
>
> on every C source file you've got.
>
> Then, run for example:
>
> gcc foo.o bar.o -lm
>
> This will produce the file a.out (use -o <outfile> to name it
> differently) by linking the 2 object files foo.o and bar.o together
> and also linking with the math library when necessary (-lm means to
> search for libm.so or libm.a).
>
> You should have a look at "Advanced Linux Programming". It gets really
> 'advanced' on some subjects, but it starts out with the basics:
>
> http://www.advancedlinuxprogramming.com/
>
> HTH
> Claudio
>
^ permalink raw reply [flat|nested] 26+ messages in thread
end of thread, other threads:[~2002-04-30 2:51 UTC | newest]
Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-09-12 15:32 Newbie gcc question Wayne Willson
1999-09-12 15:47 ` Tim Prince
1999-09-30 23:56 ` Tim Prince
1999-10-01 0:00 ` Tim Prince
1999-09-12 21:47 ` Chris Gregory
1999-09-30 23:56 ` Chris Gregory
1999-10-01 0:00 ` Chris Gregory
1999-09-30 23:56 ` Wayne Willson
1999-10-01 0:00 ` Wayne Willson
1999-10-13 15:54 newbie " Maggie Owens
1999-10-13 16:09 ` Matthew Majka
1999-10-14 0:05 ` Johan Nilson
1999-10-14 9:58 ` Matthew Majka
1999-10-31 13:57 ` Matthew Majka
1999-10-31 13:57 ` Johan Nilson
1999-10-31 13:57 ` Matthew Majka
1999-10-13 22:53 ` Konstantinos Adamopoulos
1999-10-31 13:57 ` Konstantinos Adamopoulos
1999-10-14 12:25 ` Maggie Owens
1999-10-31 13:57 ` Maggie Owens
1999-10-31 13:57 ` Maggie Owens
2002-04-29 18:21 Michael Kahle
2002-04-29 18:45 ` Claudio Bley
2002-04-29 19:48 ` Andrea 'Fyre Wyzard' Bocci
2002-04-29 19:51 Michael Kahle
2002-04-29 20:14 Michael Kahle
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).