PDA

View Full Version : [ubuntu] [SOLVED] gcc: no input files



ms2756
January 4th, 2009, 11:51 PM
Hi all,

I have spent a decent amount of time trying to get my gcc to work, but it won't. Here's what I tried:

sudo apt-get install gcc
//told me I had the latest version

gcc -o HelloWorld.c
gcc: no input files

gcc -Wall -ansi HelloWorld.c
//a bunch of stuff, like
HelloWorld.c:7: error: 'cout' undeclared (first use in this function)

I also used Tab to complete program name, so there are no errors of that sort. Also gcc complained at some point about not recognizing <stdio.h>

Here is my program (copied directly from a book):

include <iostream>

using namespace std;

int main()
{
cout << "Hello World!" << endl;
return 0;
}


Thank you very much for any suggestions.
Side note: I have Ubuntu 8.10, installed it yesterday, so should be up to date.

igknighted
January 4th, 2009, 11:55 PM
gcc -o file.c will try to output a file called file.c, and no input file was given. You would probably want 'gcc file.c -o file.bin', which would input the file file.c, and output the file file.bin

EDIT: replace those first two lines in your file with '#include <stdio.h>'

What book are you using? That's some pretty odd looking C code...

EDIT #2: You might need some additional libraries to compile... try installing the package build-essential too

anewguy
January 5th, 2009, 12:00 AM
I'm trying to remember, but isn't cout a C++ function? If so, have you tried using g++ in place of gcc?

Dave

MighMoS
January 5th, 2009, 12:04 AM
Hi all,

I have spent a decent amount of time trying to get my gcc to work, but it won't. Here's what I tried:

sudo apt-get install gcc
//told me I had the latest version

gcc -o HelloWorld.c
gcc: no input files

gcc -Wall -ansi HelloWorld.c
//a bunch of stuff, like
HelloWorld.c:7: error: 'cout' undeclared (first use in this function)

I also used Tab to complete program name, so there are no errors of that sort. Also gcc complained at some point about not recognizing <stdio.h>

Here is my program (copied directly from a book):

include <iostream>

using namespace std;

int main()
{
cout << "Hello World!" << endl;
return 0;
}


You need g++. Also, you have a .c extension, which the compiler will assume to be C (not C++). Using .cc or .cpp is recommended, instead. So in the end your command to compile and run your program will look something like
g++ HelloWorld.cc -o HelloWorld
./HelloWorld

ken22
January 5th, 2009, 12:14 AM
There is nothing wrong with your original code, it's just not C it's C++.

g++ is the linux c++ compiler.

ms2756
January 5th, 2009, 01:52 AM
Thanks to all. I changed my code to the following and I installed/compiled with g++.


#include <stdio.h>

int main()
{
cout << "Hello World!" << endl;
return 0;
}

I got:
HelloWorld.cpp: In function 'int main()':
HelloWorld.cpp:5: error: 'cout' was not declared in this scope
HelloWorld.cpp:5: error: 'endl' was not declared in this scope

I get the same errors when I follow MighMoS' suggestions: changing to .cc or .cpp makes no difference, nor does -o.
But thanks anyway.

Any suggestions?

ken22
January 5th, 2009, 02:02 AM
Yes,

If you want this to compile as C++, try:



#include <iostream>

using namespace std;

int main()
{
cout << "Hello World!" << endl;
return 0;
}

stdio.h is a C header not C++

cout is defined in iostream and you must declare that you're using namespace std;

I use the extension .cpp as it is standard practice, at least where I work.

ken22
January 5th, 2009, 02:07 AM
A C version would be



#include <stdio.h>

int main()
{
printf("Hello World");
return 0;
}

igknighted
January 5th, 2009, 02:11 AM
Thanks to all. I changed my code to the following and I installed/compiled with g++.


#include <stdio.h>

int main()
{
cout << "Hello World!" << endl;
return 0;
}

I got:
HelloWorld.cpp: In function 'int main()':
HelloWorld.cpp:5: error: 'cout' was not declared in this scope
HelloWorld.cpp:5: error: 'endl' was not declared in this scope

I get the same errors when I follow MighMoS' suggestions: changing to .cc or .cpp makes no difference, nor does -o.
But thanks anyway.

Any suggestions?

I made those suggestions assuming you were trying to compile C code, you should leave it if using C++.

ms2756
January 5th, 2009, 02:36 AM
Thanks a lot everybody. It did compile and gave out a.out.
Before I just naturally assumed that C++ encompasses C, just like Java contains all of its previous versions (for the most part). I guess C++ !> C.

Now what do I do with a.out? Apparently, I don't have an "exec" function.
Is there something else? Tried apropos exec - not useful.
Suggestions?

igknighted
January 5th, 2009, 02:37 AM
Thanks a lot everybody. It did compile and gave out a.out.
Before I just naturally assumed that C++ encompasses C, just like Java contains all of its previous versions (for the most part). I guess C++ !> C.

Now what do I do with a.out? Apparently, I don't have an "exec" function.
Is there something else? Tried apropos exec - not useful.
Suggestions?

./a.out to run the file.

ken22
January 5th, 2009, 02:38 AM
Now what do I do with a.out? Apparently, I don't have an "exec" function.
Is there something else? Tried apropos exec - not useful.
Suggestions?

To run a.out


./a.out

from the directory it is in.

ms2756
January 5th, 2009, 02:40 AM
Yup, thanks.
How do I close this question? (I heard some people complain about resolved questions still being open).

igknighted
January 5th, 2009, 02:43 AM
At the top of the page, click "thread tools" -> Marked as solved