PDA

View Full Version : [ubuntu] G++/C++ Help



The13thbrother
March 8th, 2009, 01:34 AM
Hey, I'm very new to Ubuntu and C++. Having only installed Ubuntu yesterday and tried out c++ about 10 minutes ago. I installed g++ to make my programs work and when i try to run it using terminal i type in this:

"g++ hello.cpp -o hello" (no quotes)

i get this:

"g++: hello.cpp: No such file or directory
g++: no input files"


here is my program:

//include this for cout
#include <iostream.h>

int main() {

//print out the text string, "Hello, World!"
cout << "Hello, World!" << endl;

return 0;

}

and i am saving to desktop

am i typing something wrong, am i saving wrong? do i need to save to a g++ folder and if i do, how?

thanks for your help!

DGortze380
March 8th, 2009, 01:39 AM
open a terminal and type

cd Desktop

then try g++ again.

You need to run g++ in the directory in which your source file is located.

OR

Use absolute paths (replace <> with your username)

g++ /home/<username>/Desktop/hello.cpp -o /home/<username>/Destkop/hello



PS-

You're going have compile errors.
You should choose one fo the following options for your source code



//include this for cout
#include <iostream.h>

using namespace std;

int main() {

//print out the text string, "Hello, World!"
cout << "Hello, World!" << endl;

return 0;

}


OR



//include this for cout
#include <iostream.h>

int main() {

//print out the text string, "Hello, World!"
std::cout << "Hello, World!" << endl;

return 0;

}


Last tip. <iostream.h> is typically used in C, you should use <iostream> for ANSI C++ code.

ekaqu
March 8th, 2009, 01:40 AM
here is my program:

//include this for cout
#include <iostream.h>

int main() {

//print out the text string, "Hello, World!"
cout << "Hello, World!" << endl;

return 0;

}

your code is the issue.

#include <iostream.h> //this is c style
#include <iostream> //this is c++ style

using namespace std; //needed to use anything from std libs



//include this for cout
#include <iostream>
using namespace std;
int main() {

//print out the text string, "Hello, World!"
cout << "Hello, World!" << endl;

return 0;

}

Use this code and it will work. c++ has some small differences from c that you have to watch out for.