PDA

View Full Version : How do I use g++?



s_h_a_d_o_w_s
July 12th, 2006, 08:23 PM
I have my .cpp file, how do I make g++ turn it in to a executable file? What file type will it be? .exe? .tar.bz2?

mgor
July 12th, 2006, 08:26 PM
in a terminal,

g++ source.cpp
will compile a binary file named 'a.out', look in the current directory for it, you can execute it with,

./a.out

for more information, read the manual page for g++.

s_h_a_d_o_w_s
July 12th, 2006, 08:57 PM
what if i want a .exe?

ifokkema
July 12th, 2006, 09:03 PM
what if i want a .exe?
.exe is a WINDOWS executable name. In Linux, you don't have to call your file with any extension. You could

g++ -o file.exe file.cpp
but that will not make your executable work on Windows.

mostwanted
July 12th, 2006, 09:20 PM
The default is to not have any extension in UNIXes


g++ -o myprogram source.cpp

s_h_a_d_o_w_s
July 12th, 2006, 09:46 PM
How can I tell g++ to use another name? Every time it writes over a.out , but say if i want b.out or numberdoubler.out ?

ifokkema
July 12th, 2006, 09:50 PM
How can I tell g++ to use another name? Every time it writes over a.out , but say if i want b.out or numberdoubler.out ?

Like I (and mostwanted) said:



g++ -o file.exe file.cpp
So

g++ -o <name_you_want_for_your_outfile> <your_source.cpp>
g++ -o numberdoubler.out source.cpp
g++ -o my_executable source.cpp
etc, etc...

mostwanted
July 12th, 2006, 09:53 PM
Might wanna do a


man g++

as well.

s_h_a_d_o_w_s
July 12th, 2006, 10:11 PM
thanks a lot! I just used it to write my first program. I am learning C++, and I couldn't find any good compilers. Seems like noone really has any gui for such programs.

Somenoob
July 13th, 2006, 02:58 AM
What other programming languages can it compile files from?

ifokkema
July 13th, 2006, 07:55 AM
What other programming languages can it compile files from?
Just C and C++. See man g++:

gcc - GNU project C and C++ compiler

thumper
July 13th, 2006, 09:49 AM
The gnu compiler collection can also do ADA, fortran, java, and I'm sure there is more.

asimon
July 13th, 2006, 12:42 PM
The gnu compiler collection can also do ADA, fortran, java, and I'm sure there is more.
You've forgot Objective C (http://en.wikipedia.org/wiki/Objective-C). ;-)

s_h_a_d_o_w_s
July 16th, 2006, 08:13 PM
Can I update g++? I don't think it's up to ANSI standards or something, in the compile error log, it wants me to add lots of semi-colons in front of cout , and in my c++ book, c++ for dummies, they're not ther?

ifokkema
July 16th, 2006, 08:18 PM
Could you post the error message and the respective piece of code?

s_h_a_d_o_w_s
July 17th, 2006, 12:29 AM
Sure, here it is. But I should say I started learning two weeks ago, well, and i'm 13 yrs old.

Source:

#include <iostream>
using namespace std;

int main()
{

int orgnum;

cout << "Enter a number for me to double... ";
cin >> orgnum;

int dorgnum;
dorgnum = orgnum * 2

cout << "Double your original number is: ";
cout << dorgnum << endl;

system("PAUSE");
return 0;
}



and the error log:

ubuntu@ubuntuzilla:~$ g++ /home/ubuntu/Desktop/numdoubler.cpp
/home/ubuntu/Desktop/numdoubler.cpp: In function ‘int main()’:
/home/ubuntu/Desktop/numdoubler.cpp:15: error: expected `;' before ‘cout’


If I am correct, in my book there are never any semi-colons in front of cout. Maybe I am wrong.

EchoRevamped
July 17th, 2006, 05:53 AM
You need a semicolon after "dorgnum = orgnum * 2". Simple mistake, we all make them. :)

s_h_a_d_o_w_s
July 17th, 2006, 07:32 PM
Ooops, thanks. guess I was going too fast.

s_h_a_d_o_w_s
July 17th, 2006, 07:41 PM
Do you know of any simple programs for writing and compiling? I need programs that i can destribute, like. deb or something. And are there any for MS? I've tried anjuta, eclipse, etc. but they're too complicated. I just want to write the code and click a ''compile'' button.

ifokkema
July 17th, 2006, 08:27 PM
I've never used it, but I believe Codeblocks (http://www.codeblocks.org/) does what you need.

sjenks1
July 17th, 2006, 08:39 PM
Hi

g++ is best used from the command line i.e.

g++ -g -Wall -o<file1> <file1.cc>

file1 = name you want to output to (the executable dont include an EXE extension :D )

file1.cc is whatever your source code is called with the .cc extension

----------

you can try man g++ on the command line to learn the different bits of the compiler :-D

If my memory serves me right its best to use #include<iostream.h> as well ??