Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old May 9, 2005, 12:48 PM   #1
Soulstorm
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Compilation of C++ in Terminal

How Do I compile simple .cpp files with the terminal? I know I can use xCode for this, but I want to do it with the terminal.

I used "gcc <<filepath>>" but it shows errors of undefined symbols! Anyway, can you tell me how can I do it?
Soulstorm is offline   0 Reply With Quote
Old May 9, 2005, 01:01 PM   #2
pepeleuepe
macrumors 6502
 
Join Date: Oct 2002
Location: Los Angeles, California
try using:

Code:
g++ filename
this will use the C++ compiler for GCC which should make your code compile correctly. I thought gcc would do the same thing, but it might cause some problems, so try the g++ command.

Hope that helps
__________________
Play Ultimate
pepeleuepe is offline   0 Reply With Quote
Old May 9, 2005, 01:03 PM   #3
evilpenguin21
macrumors member
 
Join Date: Jul 2002
^Edit: what he said^
evilpenguin21 is offline   0 Reply With Quote
Old May 10, 2005, 05:52 PM   #4
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by pepeleuepe
try using:

Code:
g++ filename
this will use the C++ compiler for GCC which should make your code compile correctly. I thought gcc would do the same thing, but it might cause some problems, so try the g++ command.

Hope that helps
Indeed it helped. Now it compiles correcty. How could I have imagined to use g++ instead of gcc?? Where does apple say something like that?

And why gcc doesn't work?
Soulstorm is offline   0 Reply With Quote
Old May 10, 2005, 06:09 PM   #5
daveL
macrumors 68020
 
daveL's Avatar
 
Join Date: Jun 2003
Location: Montana
Quote:
Originally Posted by Soulstorm
Indeed it helped. Now it compiles correcty. How could I have imagined to use g++ instead of gcc?? Where does apple say something like that?

And why gcc doesn't work?
First, Apple has nothing to do with it; gcc/g++ are GNU open source projects which Apple has leveraged. You might notice the similarity between cc <-> gcc, and c++ <-> g++. A "C" compiler and a "C++" compiler.
__________________
-daveL
daveL is offline   0 Reply With Quote
Old May 10, 2005, 06:18 PM   #6
therevolution
macrumors 6502
 
Join Date: May 2003
Quote:
Originally Posted by Soulstorm
And why gcc doesn't work?
gcc is the C compiler. g++ is the C++ compiler.

C++ has features that C does not. Consequently, the C compiler won't understand C++-specific code.
therevolution is offline   0 Reply With Quote
Old May 10, 2005, 06:44 PM   #7
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
Quote:
Originally Posted by Soulstorm
And why gcc doesn't work?
To add to whatever everyone said, C is a subset of C++ therefore a C compiler would not be able to parse C++.
superbovine is offline   0 Reply With Quote
Old May 11, 2005, 12:00 AM   #8
NuPowerbook
macrumors member
 
Join Date: Jan 2004
Quote:
Originally Posted by superbovine
To add to whatever everyone said, C is a subset of C++ therefore a C compiler would not be able to parse C++.
More technically isn't C++ a superset of C? (since C came first).
NuPowerbook is offline   0 Reply With Quote
Old May 11, 2005, 02:33 AM   #9
Soulstorm
Thread Starter
macrumors 68000
 
Soulstorm's Avatar
 
Join Date: Feb 2005
Quote:
Originally Posted by therevolution
gcc is the C compiler. g++ is the C++ compiler.

C++ has features that C does not. Consequently, the C compiler won't understand C++-specific code.
I just thought gcc was the c++ compiler, not the plain c.
Soulstorm is offline   0 Reply With Quote
Old May 11, 2005, 02:51 AM   #10
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
Quote:
Originally Posted by NuPowerbook
More technically isn't C++ a superset of C? (since C came first).
I guess you are right.
superbovine is offline   0 Reply With Quote
Old May 11, 2005, 03:04 AM   #11
cube
macrumors G3
 
Join Date: May 2004
Quote:
Originally Posted by Soulstorm
I used "gcc <<filepath>>" but it shows errors of undefined symbols! Anyway, can you tell me how can I do it?
If you have undefined symbols, is because you are missing some libraries in the command that you must specify with the -l options (you may also have to specify in which directories to look for with -L options).

If this is someone else's program, there's probably a makefile to execute with 'make'.

If this is some open source file there might be a 'configure' script to create makefiles/set up toptions according to the platform. Just executing 'configure' without options might work. (Always look for a README or INSTALL file with explanations, BTW).
cube is offline   0 Reply With Quote
Old May 11, 2005, 03:09 AM   #12
cube
macrumors G3
 
Join Date: May 2004
Both gcc and g++ are C/C++ compilers.

gcc assumes preprocessed (.i) files are C and assumes C style linking.
g++ assumes preprocessed (.i) files are C++ and assumes C++ style linking.
cube is offline   0 Reply With Quote
Old May 12, 2005, 09:59 AM   #13
therevolution
macrumors 6502
 
Join Date: May 2003
Quote:
Originally Posted by Soulstorm
I just thought gcc was the c++ compiler, not the plain c.
Nope; by default, gcc parses files as C files.

In reality, I believe 'g++' is effectively the same as 'gcc -x c++'. In other words, it's gcc in "C++ mode."
therevolution is offline   0 Reply With Quote
Old May 12, 2005, 10:48 AM   #14
ChrisBrightwell
macrumors 68020
 
ChrisBrightwell's Avatar
 
Join Date: Apr 2004
Location: Huntsville, AL
Quote:
Originally Posted by therevolution
gcc is the C compiler. g++ is the C++ compiler.
This isn't exactly true.

http://en.wikipedia.org/wiki/Gcc

Quote:
The GNU Compiler Collection (usually shortened to GCC) is a set of programming language compilers produced by the GNU Project. It is free software distributed by the Free Software Foundation (FSF) under the GNU GPL, and is a key component of the GNU toolchain. It is the standard compiler for the open source Unix-like operating systems, and certain proprietary operating systems derived therefrom such as Mac OS X.

Originally named the GNU C Compiler, because it only handled the C programming language, GCC was later extended to compile C++, Fortran, Ada, and others.

HTH.
__________________
"Simplicity is the ultimate sophistication."
ChrisBrightwell is offline   0 Reply With Quote
Old May 12, 2005, 11:10 AM   #15
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
From man gcc/g++:

Compiling C++ Programs

C++ source files conventionally use one of the suffixes .C, .cc, .cpp,
.c++, .cp, or .cxx; preprocessed C++ files use the suffix .ii. GCC
recognizes files with these names and compiles them as C++ programs
even if you call the compiler the same way as for compiling C programs
(usually with the name gcc).

However, C++ programs often require class libraries as well as a com-
piler that understands the C++ language---and under some circumstances,
you might want to compile programs from standard input, or otherwise
without a suffix that flags them as C++ programs. g++ is a program
that calls GCC with the default language set to C++, and automatically
specifies linking against the C++ library. On many systems, g++ is
also installed with the name c++.
__________________
Someone get Nekkid™, now!

Last edited by gekko513; May 12, 2005 at 11:12 AM.
gekko513 is offline   0 Reply With Quote
Old May 13, 2005, 10:33 AM   #16
therevolution
macrumors 6502
 
Join Date: May 2003
Quote:
Originally Posted by ChrisBrightwell
This isn't exactly true.

http://en.wikipedia.org/wiki/Gcc




HTH.
Yeah, I know. Read my comment right above yours. I thought it would be less confusing for a newbie to think of them as separate.
therevolution is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Help compiling/running graphics in terminal dflem11 Mac Programming 2 Nov 8, 2010 02:42 PM
how to compile Obj-c file in terminal uaecasher Mac Programming 18 Jul 30, 2009 08:41 AM
running compiled C++ executables in Terminal cossie Mac Programming 2 Mar 29, 2007 05:53 AM
how to get number of lines in terminal wala Mac Programming 1 Dec 3, 2006 09:59 PM
Batch execution of commands in Terminal? typecase OS X 3 May 12, 2005 08:18 PM


All times are GMT -5. The time now is 05:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC