LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Why I can't compile my c program? (https://www.linuxquestions.org/questions/programming-9/why-i-cant-compile-my-c-program-391813/)

zhuqlfeixia 12-12-2005 02:27 AM

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!

Mistro116@yahoo.com 12-12-2005 02:57 AM

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.

pddm 12-12-2005 03:16 AM

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>

smurff 12-12-2005 03:41 AM

Hi,

Just to kind of repeat what pddm said.

hello.c:1

the number after : is the line number the error is on.

graemef 12-12-2005 07:10 AM

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.

sirclif 12-12-2005 12:36 PM

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...

zhuqlfeixia 12-12-2005 09:13 PM

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.