Compilation of C++ in Terminal

Discussion in 'Mac Programming' started by Soulstorm, May 9, 2005.

  1. macrumors 68000


    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?
  2. macrumors 6502

    try using:

    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 :)
  3. macrumors member

    ^Edit: what he said^
  4. macrumors 68000


    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?
  5. macrumors 68020


    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.
  6. macrumors 6502

    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.
  7. macrumors 68030


    To add to whatever everyone said, C is a subset of C++ therefore a C compiler would not be able to parse C++.
  8. macrumors member

    More technically isn't C++ a superset of C? (since C came first).
  9. macrumors 68000


    I just thought gcc was the c++ compiler, not the plain c.
  10. macrumors 68030


    I guess you are right.
  11. macrumors G3

    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).
  12. macrumors G3

    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.
  13. macrumors 6502

    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."
  14. macrumors 68020


    This isn't exactly true.

  15. macrumors 603


    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++.
  16. macrumors 6502

    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.

Share This Page