Compiling using g++ in Ubuntu

Hello!

Today, I've just installed Ubuntu last night and now I'm trying to follow some C++ tutorials, but in order to do that I need a working compiler...

I've done all the commands needed to get g++ working in terminal :
sudo apt-get install build essentials
sudo apt-get install g++

And both worked flawlessly, IMO.

Now I made a .cpp document, and dropped it on my desktop. I go back into terminal and type the following : g++ Desktop/HelloWorld.cpp -o HelloWorld.o and I hit enter. All this does it send me on to the next line.

Example :
****@ubuntu:~$ g++ Desktop/HelloWorld.cpp -o HelloWorld.o
***@ubuntu:~$ []

meaning it doesnt show me that I've done anything..

I then try : g++ HelloWorld.cpp and all I get are a ton of errors. Any help would be appreciated. I posted this in the Linux section but apparently not as many people browsing that, as there is this one.

Oops forgot my code I'm using, sorry.
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World\n";
   cin.get();
}
Last edited on
it doesnt show me that I've done anything..

That means there was no error and it has done what you told it to successfully.

The file HelloWorld.o has been created in your home directory. Run the program by typing ./HelloWorld.o and press enter.
... I feel like an idiot. Not anywhere did I see that. Thanks!
You probably don't need line 8 - that is a thing windows users need because MS VS & other IDE's closes the window immediately. This doesn't happen on linux AFAIK.

Maybe we can get into some good habits right from the start:

Don't use the std namespace like that - it brings in untold stuff into the global namespace - causing variable or function name conflicts.

Do one of these instead:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <iostream>

using namespace std;

int main()
{
   std::cout << "Hello World\n";

    std::cout << std::endl;  // endl is similar to \n but it flushes the buffer
   cin.get();

    return 0; //if all is well, something else otherwise
}


Or this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
    cout << "Hello World\n";

    cout << "More Text "<< endl;  // endl is similar to \n but it flushes the buffer
   

    return 0; //if all is well, something else otherwise
}


I tend to do a mixed approach - the using directives for things used frequently, and std:: for other things not so frequent. It is a personal preference - some people use std:: for everything.

Another thing about cout is that it can be built up in stages, Instead of this:

 
cout << "The quick brown fox jumped over the lazy dog, and had a great time doing it because it was a nice day, and it was fun to be out in the sunshine  and annoy the hell out of the dog which deserves it\n";


do this, which is equivalent:

1
2
3
4
5
6
cout << "The quick brown fox jumped over the lazy dog,";
cout << " and had a great time doing it because";
cout << " it was a nice day, and it was fun ";
cout << "to be out in the sunshine and annoy "
cout << "the hell out of the dog which deserves it." << endl;


I get irritated by newbies who post 200 char or more long cout statements. Not you - just trying to mention a good thing from the start. There is a bit of a rule that a line of code should not exceed 80 chars, and functions should not be more than 80 lines - for readability. You can see what it does to the display of the post.

Anyway Good luck!!!
Last edited on
****@ubuntu:~$ g++ Desktop/HelloWorld.cpp -o HelloWorld.o

You don't need the .o in the file name. Object files (file.o) are produced by the -c switch but that's not something you should worry about now. -o produces an executable, for which the plain file name is enough.
Topic archived. No new replies allowed.