Click to See Complete Forum and Search --> : problem compiling c program
owen76
December 7th, 2001, 04:17 PM
With everyone's help I finally got my first linux installation working. I hadn't programmed in c in two years, so anxious to get started. I typed in my starter program in pico.
#include<stdio.h>
void main()
{
printf("hello");
}
I then compiled hello.c:
gcc hello.c
It spewed back an error message, something about main not being int.
What am I doing wrong? Thanks for your help!
pwaring
December 7th, 2001, 07:29 PM
You need to change the line: 'void main()' to 'int main()' (without the quotes). This defines the main() function as returning an integer value.
You should also add the line 'return 0;' (again without the quotes) after 'printf("Hello");
A properly working example is shown below:
/* hello.c */
#include <stdio.h>
int main()
{
printf("Hello");
return 0;
}
/* end hello.c */
This should compile properly under any ASCII compliant C compiler (including gcc).
Hope this helps. :D
PainShock
December 16th, 2001, 10:24 AM
Probre in this form:
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
printf("Hello World\n");
}
/* -------------- End code -------------------- */
You also can put at this form
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
system("clear"); /* With this line when the program run first
clean the screen and next put Hello Worl. With the system you can put commands of the OS like ls, cat .... */
printf("Hello World\n");
}
/* -------------- End code -------------------- */
To compile you can put of this form:
gcc -o hello hello.c
The option -o is to put name that you want to have the exit program
Bye and lucky programing bye ;)
owen76
January 10th, 2002, 04:25 PM
Maybe I'm doing something wrong, but I tried the first approach and got an error message about printf not being recognized.
I tried gcc -c , to stop after compiling, but before linking, and manually linked with ld. That's when I got the error message. The message wouldn't show up
with just gcc. It is quite frustrating. I want to like the thing.
l3aDmOnKeY
January 10th, 2002, 06:27 PM
// little help
#include <stdio.h>
int main()
{
printf ("Hello World\n");
return 0;
}
Vorlin
January 10th, 2002, 07:19 PM
Let's see here, compilers..woo!
#include <stdio.h>
int main() {
printf("Hello world!\n");
}
That's all you need, say, in a foo.c file, of which then we do this:
$ gcc foo.c -o foo
You should have a file called 'foo' now, fully executable and ready to run. That's all that really needs to be done. By the way, the term 'void' means you don't require anything to be returned to you whereas 'int' means the result will be an integer value. Boolean means a true or false, etc etc...
PainShock
January 12th, 2002, 02:32 PM
Send your code to see how is your program and if see some bug i can tell you bye:cool:
owen76
January 17th, 2002, 05:49 PM
OK, this is a REALLY stupid question, but just typing foo at command prompt would run the program? Mine has that color coded thing going on so when the name is green, I should be executable?
rcgreen
January 18th, 2002, 12:35 AM
That's ./filename unless the current directory
is on the PATH.
Unlike DOS, Linux doesn't execute programs
located in the current directory by default.
:cool:
Terr
January 18th, 2002, 03:50 AM
Originally posted by owen76
OK, this is a REALLY stupid question, but just typing foo at command prompt would run the program? Mine has that color coded thing going on so when the name is green, I should be executable?
So you're using the "ls --color" option (or equivalent) as an alias?
Just BTW folks, you can use the "php" tags in the forum, which causes auto-formatting and color coding. (The "code" tags preserve original formatting.) For example (from Painshock's Post #3)
/* --------------- Begin code ----------------- */
#include<stdio.h>
main()
{
system("clear"); /* With this line when the program run first
clean the screen and next put Hello Worl. With the system you can put commands of the OS like ls, cat .... */
printf("Hello World\n");
}
/* -------------- End code -------------------- */
owen76
January 22nd, 2002, 04:23 PM
Yes, it did it automatically when I installed linux. Also, I copied a program from CD, but when I
tried to run the sucker it didn't work. The green files. What the hell am I doing wrong?
5150
January 22nd, 2002, 05:26 PM
i always liked the c++ way of doing things
#include <iostream.h>
int main (){
cout>>"Hello world\n";
return 0;}
i haven't tried it in linux so i don't even know if i'm right!
duz733
January 22nd, 2002, 05:49 PM
Why the hell is this so complicated?
The question I believe was simple. The answers were many and yet misguiding....
Damn you lamers!
PainShock
January 22nd, 2002, 10:13 PM
[color=blue]I try to explain how to do a C program and compiler in Linux step by step, I hope that with this you can compile a program in C.
1) First use editor to write the program for example pico
pico hello.c
2) Write the program:
#include<stdio.h>
main ()
{
system("cls");
printf("\nHello world");
}
3) Compile the program:
gcc hello.c -o hello
4) Execute the program
./hello
This is all the steps you have to do I hope you compile your program by and good luck :cool: [color=blue]
gizmofreak
November 29th, 2003, 07:32 PM
Check if all ur library files are in your include folder