PDA

View Full Version : [ubuntu] Compiling C++ in Ubuntu



DeLaney
January 25th, 2009, 07:34 PM
I've learned a small bit of C++ in Windows, but coming to Linux I have no idea how to compile programs, or if I should be writing them in anything specific. Also, the book I have tells me that I'll get an EXE. file after using a compiler. How will this work in Ubuntu?

igknighted
January 25th, 2009, 07:48 PM
I've learned a small bit of C++ in Windows, but coming to Linux I have no idea how to compile programs, or if I should be writing them in anything specific. Also, the book I have tells me that I'll get an EXE. file after using a compiler. How will this work in Ubuntu?

install the package 'build-essential', then use the command 'g++' to compile. If you were using Cygwin and GCC in windows, it will be exactly the same, aside from the filename of the output. In windows, you get a.exe, while in linux you get a.out (unless you use the -o flag to manually set the name). To run the program, type './a.out'.

DeLaney
January 26th, 2009, 12:20 AM
Cool, thanks.

I have the build-essential package, and I've tried using the g++ command to compile, I'm unsure how. I tried putting g++ and then the path to the file directory, and it said;

g++: home/zack/Game_Programming/Hello: No such file or directory
g++: no input files

Is there something else I have to put before the directory or file?

sylvainrb
January 26th, 2009, 04:50 AM
Make sure that you are compiling a .cpp file. Here's what I use to compile and run:

g++ filename.cpp -o name
./name

igknighted
January 26th, 2009, 04:55 AM
Cool, thanks.

I have the build-essential package, and I've tried using the g++ command to compile, I'm unsure how. I tried putting g++ and then the path to the file directory, and it said;

g++: home/zack/Game_Programming/Hello: No such file or directory
g++: no input files

Is there something else I have to put before the directory or file?

If you are going to use the full path to the file, it needs to start with a "/". It appears the command you should be using (by the path posted above): "g++ /home/zach/Game_Programming/Hello". Also, you should end c++ source files with .cpp or .cc.

DeLaney
January 26th, 2009, 10:03 PM
Awesome, thanks! It works now, it's just giving me error messages on bad code, which I can deal with. Thank you!

Well, I fixed up the code, and it compiled into a .out file, but I can't seem to open it. Is there a specific program I need?

snova
January 26th, 2009, 11:47 PM
Awesome, thanks! It works now, it's just giving me error messages on bad code, which I can deal with. Thank you!

Well, I fixed up the code, and it compiled into a .out file, but I can't seem to open it. Is there a specific program I need?

"Open" it? If you're clicking on it, anything it reads/writes from the terminal will go off into nothingness. You need to run it from a terminal. One will not be created automatically.

If you're already trying to run it from a terminal, what precise command are you using?

DeLaney
January 27th, 2009, 12:56 AM
Ah... I had no idea that I was supposed to run my programs from the terminal as well. How would I do that?

Sealbhach
January 27th, 2009, 01:07 AM
Ah... I had no idea that I was supposed to run my programs from the terminal as well. How would I do that?

To run any app, just type its name in the terminal. Try it with "firefox".


.

Strohfeuer
January 27th, 2009, 01:17 AM
my 2 cents ;)

http://ubuntuforums.org/showthread.php?p=6609233#post6609233

mrbiggbrain
January 27th, 2009, 01:20 AM
To run any app, just type its name in the terminal. Try it with "firefox".


.

but firefox has a symbolic link that makes firefox point to somewhere. this is NOT the case for files you compile yourself

do


#./file-name

where . is the directory the program is in. you can feel free to name files whatever you please, as unlike windows, linux does not determine if a file is execuble based upon its extention (.exe, .com)

DeLaney
January 27th, 2009, 01:41 AM
Alright, thanks guys! I managed to get it to run, so I think I should be set. Thanks for the tip on other editor too, I'll give it a shot with my next program.

matthew.ball
January 27th, 2009, 06:36 AM
Geany and Kate (are two editors I know of), which include a terminal in a text-editor environment (might even be possible to set-up a compiler for either, I don't know).

Unless you don't mind running a terminal with your text-editor, it's just useful to have it all in the one application in my experience.