Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
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?
 

pepeleuepe

macrumors 6502
Oct 27, 2002
252
0
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 :)
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
pepeleuepe said:
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?
 

daveL

macrumors 68020
Jun 18, 2003
2,425
0
Montana
Soulstorm said:
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.
 

therevolution

macrumors 6502
May 12, 2003
468
0
Soulstorm said:
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.
 

NuPowerbook

macrumors member
Jan 17, 2004
51
0
superbovine said:
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).
 

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
therevolution said:
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.
 

cube

Suspended
May 10, 2004
17,011
4,972
Soulstorm said:
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

Suspended
May 10, 2004
17,011
4,972
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.
 

therevolution

macrumors 6502
May 12, 2003
468
0
Soulstorm said:
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."
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
therevolution said:
gcc is the C compiler. g++ is the C++ compiler.
This isn't exactly true.

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

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.
 

gekko513

macrumors 603
Oct 16, 2003
6,301
1
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++.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.