4

I just need to make clear one thing. In University we are learning the C++ programming language and they suggest us to use the GNU C++ Compiler which is part of the GCC. So on my Mac OS X Mavericks I download the command line tools from the developers.apple.com. I wrote a simple C++ program and I compile this program using the g++ command like this:

g++ program.cpp
./a.out

And the program runs perfect. But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler, so while in the University we use the "GNU C++ compiler", I just want to make clear that with the g++ command is meant that I use the "GNU C++ Compiler".

Cheers.

Dr. Programmer
  • 368
  • 1
  • 7
  • 19
  • 8
    you can use `g++ --version` to check if you are using GNU C++. In my OS X Mavericks, the `g++` is not `GNU C++`, it is `clang++` – mitchelllc Jan 27 '14 at 17:52
  • `Configured with: --prefix=/Applications/Programming/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix` That's the result of the command. – Dr. Programmer Jan 27 '14 at 17:55
  • same result. It's `clang++`, not `GNU C++` – mitchelllc Jan 27 '14 at 17:56
  • 2
    That isn't exactly the same as `g++`; it is the LLVM compiler masquerading (pretty effectively) as the GNU `g++` compiler. `/usr/bin/g++ --version` yields, for me, `Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix`. – Jonathan Leffler Jan 27 '14 at 17:56
  • "But as I know, using a different compiler, means that you have to use the correct syntax/commands/libraries for this spesific compiler" -- this is not quite true. The syntax for your C++ programs will be the same whether you use Linux & G++, OS X and Clang, or Windows and MSVC. The headers and standard libraries will also be mostly the same. – Dietrich Epp Jan 27 '14 at 17:56
  • I wouldn't worry about this, if you just learn the language, you'll harldy notice the difference. – Karoly Horvath Jan 27 '14 at 17:57
  • So I need to install the GNU C++ Compiler because I am going to make projects, and I want to use the same compiler as the marking computers. The teacher suggest for mac users to download the Apple's command line tools. So why is not the GNU version? :S – Dr. Programmer Jan 27 '14 at 17:58
  • 1
    If you are concerned, just get the [GNU C++ Compiler](https://github.com/kennethreitz/osx-gcc-installer). – Elliott Frisch Jan 27 '14 at 17:59
  • 1
    @ProgrJohn http://stackoverflow.com/questions/19535422/os-x-10-9-gcc-links-to-clang This will help you. – mitchelllc Jan 27 '14 at 18:01
  • 1
    Learn the language and the programming in general. Do not worry about the minute differences between the compilers until much later. If anything, build your projects with *all* modern compilers you can lay your hands on, each set to its maximal warning level, and make sure not a squeak comes out of any of them. – n. m. could be an AI Jan 27 '14 at 18:11
  • @n.m. While in general yes, use any and everything. There are benefits to students using a well defined and standard environment. – Peter M Jan 27 '14 at 18:30
  • Whatever compiler you end up using at home, make sure you test your program using your school environment before you turn it in. Your instructor might be understanding the first time you turn in something that supposedly worked at home but not at school, but soon the instructor will tire of it and just take points off your score instead. The syllabus probably says it's *your* responsibility to ensure your code works in the grading environment. – Rob Kennedy Jan 27 '14 at 21:37

2 Answers2

4

Traditionally, gcc and g++ are both components of the GNU C compiler suite. gcc is the C compiler, and g++ is the C++ compiler.

On current versions of Mac OS X, the commands gcc and g++ are both treated as alternate names for clang and clang++, which are components of the Clang C compiler. However, this compiler is almost entirely compatible with GCC — the few differences that do exist will almost certainly not come up in the coursework you're doing.

(The most significant difference is that Clang's diagnostics are much better: it will point out exactly where a syntax error occurs in a line, rather than just what line it's on, and it can often identify potential typos or subtle mistakes in situations where GCC would just give you a cryptic error message. If you're just learning C, you will appreciate this a lot.)

  • 4
    The stuff about better diagnostics is debatable. Now GCC also uses ASCII art to point exactly where within a line of code the error occurred. Competition is a wonderful thing :) – Praetorian Jan 27 '14 at 18:07
  • That true thank you! When I first compile the c++ program, I saw that the compiler highlights the errors with colors and it was more spesific on the errors rather than the gcc tha i use on Linux. – Dr. Programmer Jan 27 '14 at 18:11
  • 1
    @Praetorian: As I understand it, GCC's parser is (still) not awesome at identifying the "root cause" of a syntax error - it'll tell you at what point it gave up entirely, but that may be some distance away from the actual error. I have heard that it's improving, but it's still not as good as Clang, I'm afraid! –  Jan 27 '14 at 18:13
1

As per @duskwuff, I would prefer to use clang++, however if you must be compatible, then you can installi the real GNU compiler via macports.

After installing macports (which includes a xcode-select step), simply do:

$ sudo port selfupdate
$ sudo port install gcc46

(or gcc47, etc.)

The compiler will be in your $PATH (if you set-up macports correctly), but explicitly, it will be /opt/local/bin/gcc46 (see sudo port select gcc).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • why using macports when you could use homebrew? – rano Jan 27 '14 at 18:10
  • @rano Because macports has more ports and is less complicated WRT `$PATH` (as far as I understand it, at least). – trojanfoe Jan 27 '14 at 18:11
  • The `$PATH` for Homebrew is easy as π: make sure `/usr/local/bin` is in there. Bam, done. –  Jan 27 '14 at 18:13
  • I try to install via homebrew like: `homebrew install gcc46` and the return result was that the package was missing. So I will try to install it via the macports. Cheers – Dr. Programmer Jan 27 '14 at 18:15
  • After installing the gcc46 via the macport the result of the command: `g++ --version` is the same: `Configured with: --prefix=/Applications/Programming/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) Target: x86_64-apple-darwin13.0.0 Thread model: posix` – Dr. Programmer Jan 27 '14 at 18:23
  • @ProgrJohn Does your `$PATH` include `/opt/local/bin` (you might need to restart `Terminal.app`)? If so, what does `ls -l /opt/local/bin/g++*` show? If it's not `g++` then you probably need to use `port select gcc` (google that). – trojanfoe Jan 27 '14 at 22:09
  • `. . -rwxr-xr-x 4 root admin 435492 20 Nov 22:23 g++-mp-4.6 -rwxr-xr-x 3 root admin 431228 20 Nov 22:23 gcc-mp-4.6 -rwxr-xr-x 4 root admin 435492 20 Nov 22:23 c++-mp-4.6 . .` That's the result of the command (just the lines with reference to c++ or g++, gcc) – Dr. Programmer Jan 28 '14 at 13:36
  • @ProgrJohn OK, so if `/opt/local/bin` is in your `$PATH` you can invoke it using `gcc-mp-4.6` (not very attractive option), so I would use the `port select gcc` option. – trojanfoe Jan 28 '14 at 13:39
  • I get `Available versions for gcc: mp-gcc46, none (active)` if i choose the mp-gcc46 I get an error that does not exist. – Dr. Programmer Jan 28 '14 at 15:15
  • @ProgrJohn Have you seen this SO question: http://stackoverflow.com/questions/837992/update-gcc-on-osx – trojanfoe Jan 28 '14 at 15:33
  • `g++ (MacPorts gcc46 4.6.4_3) 4.6.4 Copyright (C) 2011 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ` Now my version is that one. Is it ok? moreover if I want to flip back to the pre-installed c++ compiler by apple what I have to do? Cheers again – Dr. Programmer Jan 28 '14 at 16:56
  • @trojanfoe: I do not want to start a flame, but homebrew is REALLY macports done right : ) – rano Jan 28 '14 at 19:10
  • @ProgrJohn Yeah that looks right. If you want to use the Apple compiler then use `clang` instead of `gcc`. – trojanfoe Jan 28 '14 at 20:25
  • @rano I disagree. Macports works fine for me, and has more ports, so how can it be better? – trojanfoe Jan 28 '14 at 20:26
  • 1
    @trojanfoe how do you say more ports? never trust a program that requires sudo to do that ; ) – rano Jan 28 '14 at 20:30
  • macports is a trusted author by apple so i dont think is something bad. – Dr. Programmer Jan 28 '14 at 22:07