PDA

View Full Version : [SOLVED] Help with compiling and running in g++



Raff1
June 5th, 2009, 10:32 AM
Hello!

I am new to Ubuntu and trying to figure out how I can compile .cpp-files and run stuff with g++.

I made a simple hello.cpp which cout's "Hello World!". I then compiled it using the terminal



#include <iostream>

int main(){

std::cout << "Hello World!";
std::cin.get();

}(I later added the cin.get() in order to pause the program in case it would close before I could see the message, but it did not help...)



g++ hello.cpp (or: g++ hello.cpp -o something)After compiling I find some "a.out" or "something" file, but I dont know what kind of files that is or how I can run them. I have tried double clicking on them and running them from the terminal. Whatever I do, I never spot the intended "Hello World!" message.

So what kind of files do I produce when compiling and how do I use them? How can I see the output? Once I am past this initial barrier I can start playing about with the code it self. :p

Thanks in advance,
Regards Raff1

froggyswamp
June 5th, 2009, 11:12 AM
a.out is the default executable name if you don't specify a custom one through the command line arguments.
To run it just go the folder it is in and type ./a.out + Enter. Should work, if not, sacrifice a goat during full moon and try again.

ibuclaw
June 5th, 2009, 11:59 AM
In the terminal, run

./a.out
Notice the ./ prefix, that is essential. It is how you run all executable files in your working directory in the terminal.

In Linux, gcc produces ELF binaries. (http://en.wikipedia.org/wiki/Executable_and_Linkable_Format)

Regards
Iain

MadCow108
June 5th, 2009, 01:45 PM
a reason you maybe missed the output is because the "endl" for the line break is missing:
std::cout<< "hello" << std::endl;

otherwise it appends the output to the begining of the prompt with no break.

Raff1
June 6th, 2009, 02:06 PM
In the terminal, run

./a.outNotice the ./ prefix, that is essential. It is how you run all executable files in your working directory in the terminal.

In Linux, gcc produces ELF binaries. (http://en.wikipedia.org/wiki/Executable_and_Linkable_Format)

Regards
Iain

Ah! Thank you for that very informative post! I understand a bit more about unix executables now. =) It worked like a dream once I knew how to run the file!


Should work, if not, sacrifice a goat during full moon and try again.My goat is fortunate enough to live yet another day! :p


Regards,
Raff1