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! |
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. |
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> |
Hi,
Just to kind of repeat what pddm said. hello.c:1 the number after : is the line number the error is on. |
In C the printf function is in the stdio.h file and so you want the #include to be:
Code:
#include <stdio.h> graeme. |
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... |
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:) |
All times are GMT -5. The time now is 09:14 AM. |