Hi!
I have recently started using linux and I was recommended gcc compiler.
I have written a basic program just to see if it works. And it doesn't.

I am trying to compile it with Terminal, and with the following command :

gcc ~/Desktop/C++/test.cpp -o testing)

My program it responds with:

/home/john/Desktop/C++/test.cpp:1:1: error: 'include' does not name a type

If on the other hand I write

gcc test.cpp -o testing

the result is:

gcc: test.cpp: No such file or directory
gcc: no input files

I am wondering what that means and how i could fix it.

Here is the code of my simple program

#include<iostream>
using namespace std;

int main()
{
  string name;
  cout<<"Whats your name?";
  cin >> name;
  cout << "Hello"<< name;
}

Thanks for all of your responses. I hope you help me fix this problem soon .
Until then I should use MS Windows i guess ... :S

gcc - c test.cpp
gcc - o "run command goes here" (without inverted commas) test.cpp
./"run command"

I'm not sure why it would give that specific error message; it is more typical of certain types of header problems. You might try using 'g++' rather than 'gcc'; by default, gcc assumes that you're compiling a C program, while g++ is specific to C++.

I would also recommend always using the '-Wall' switch with gcc and g++, like so:

g++ -Wall testing.cpp -o test

This option turns on all of the warnings which the compiler can give you - by default, most of the diagnostic warnings are turned off, but having them turned on is very helpful in debugging.

As for the second shell command, the reason it can't find the file is because you aren't in the directory the file is in, and you haven't told it which directory to look for it. By default, when you open a terminal in Linux, it starts off in your home directory, '/home/john/' in this instance. If the source file is in a folder on your Desktop, you need to either give it the path to the file, or else change directories so that you are in the same folder as the file. In this case, the latter can be done with the simple command cd Desktop/C++ , which should change your current working directory to '/home/john/Desktop/C++/'. If you get confused as to where you are, you can use the command pwd to get your working directory.

BTW, the tilde ('~') in the path of the first version indicates that you are working relative to your home directory. you can always get back to your home directory with the command cd ~ . To move up to the parent directory relative to the directory your now in, you would enter cd .. ; the '..' is a pointer to the parent directory all non-root directories have.

commented: informative and its solved :) +2

Hmmm, while I couldn't reproduce it, I wonder if the problem is the lack of a space between "#include" and "<iostream>" -- it sounds like it's ignoring the # and trying to do something template-like.

BTW, which version of gcc are you using? gcc --version . I have a relatively old "g++.exe (GCC) 3.4.5 (mingw-vista special r3)" because of some of the libraries I need to link against. :)

Thank you for all of your replies.
I really appreciate it.
What Schoil-R-LEA said helped me solve my problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.