LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2005, 02:27 AM   #1
zhuqlfeixia
LQ Newbie
 
Registered: Dec 2005
Posts: 19

Rep: Reputation: 0
Why I can't compile my c program?


Hello bodies!
I'm using Redhat9
And now I'm learning to program under this OS.

This is my gcc information:
[root@localhost bin]# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/3.2.2/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --host=i386-redhat-linux
Thread model: posix
gcc version 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I write a simple "Hello World!" program:
#include
int main()
{
printf("Hello World!");
exit(0);
}

I had tried two times,but it gets wrong as follow:
1.
[root@localhost GratuationDesign]# gcc hello.c
hello.c:1:10: #include expects "FILENAME" or <FILENAME>
2.
[root@localhost GratuationDesign]# gcc hello hello.c
gcc: hello: 没有那个文件或目录(means:can't find that file)
hello.c:1:10: #include expects "FILENAME" or <FILENAME>


Then I modified my program as follow:
#include <iostream>
int main()
{
printf("Hello World!");
exit(0);
}
The result is:
1.
[root@localhost GratuationDesign]# gcc hello.c
hello.c:1:20: iostream: 没有那个文件或目录(means:can't find that file)

2.
[root@localhost GratuationDesign]# gcc hello hello.c
gcc: hello: 没有那个文件或目录
hello.c:1:20: iostream: 没有那个文件或目录

I'm a new man in Linux, and I don't know what to now......
Could somebody help me? Thank you!
 
Old 12-12-2005, 02:57 AM   #2
Mistro116@yahoo.com
Member
 
Registered: Sep 2005
Posts: 118

Rep: Reputation: 15
Try #include <iostream.h> and since int main() has integer return type, include an integer return value: such as return 1; after exit, which isn't really needed.

Try:

#include <iostream.h> /* Not needed for printf */

int main ()
{
printf ("Hello World!\n");

return 0;
}

Hope this helps.
 
Old 12-12-2005, 03:16 AM   #3
pddm
Member
 
Registered: Sep 2005
Distribution: Mint 19.2
Posts: 112

Rep: Reputation: 15
Your Problem is here:

#include
int main()
{
printf("Hello World!");
exit(0);
}

the include needs a filename: ie #include <stdio.h>

this is why gcc complains with: hello.c:1:10: #include expects "FILENAME" or <FILENAME>
 
Old 12-12-2005, 03:41 AM   #4
smurff
Member
 
Registered: Sep 2004
Location: England
Distribution: Mandriva 2005LE / Whitebox
Posts: 48

Rep: Reputation: 15
Hi,

Just to kind of repeat what pddm said.

hello.c:1

the number after : is the line number the error is on.
 
Old 12-12-2005, 07:10 AM   #5
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
In C the printf function is in the stdio.h file and so you want the #include to be:
Code:
#include <stdio.h>
The iostream header file is a C++ library that manages the input output functions for C++ style streams, typically using the << or >> operator

graeme.
 
Old 12-12-2005, 12:36 PM   #6
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
yea, your mixing C and C++. it looks like your trying to write a C program since your using the C compiler (gcc), the printf() function is in the <stdio.h> header, so use the include graemef suggested. If you're trying to compile C++ code, you use the command g++ file.cpp. In which case you would use the include <cstdio> to use the printf() function.

C header files are .h, but C++ header files do not have the .h extention. the compiler will complain sometimes if you try include something like <stdio.h> and use the C++ compiler (g++). however, all C header files are available in C++, you just remove the .h from the header file and add a c to the beginning. so...

<stdio.h> becomes <cstdio>
<stdlib.h> becomes <cstdlib>
and so on...
 
Old 12-12-2005, 09:13 PM   #7
zhuqlfeixia
LQ Newbie
 
Registered: Dec 2005
Posts: 19

Original Poster
Rep: Reputation: 0
Thak you very much
I'v got it! Though I'm still not very clear,but I'will try later on.
I reply this letter late beacuse the problem 0f Time Zone. Sorry! Haha
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Compile Program mattp Linux - Newbie 4 03-09-2004 11:13 AM
program want compile dlm4444 Programming 10 09-26-2003 08:08 PM
Compile qt program. hfawzy Linux - General 2 02-18-2003 10:25 AM
compile C program juno Linux - Software 4 09-27-2002 12:29 PM
compile program Eddie9 Linux - General 1 05-21-2002 10:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration