How to Compile C++ with C++11 Support in MAC Terminal

How to compile C++ with C++11 support in Mac Terminal

As others have pointed out you should use clang++ rather than g++. Also, you should use the libc++ library instead of the default libstdc++; The included version of libstdc++ is quite old and therefore does not include C++11 library features.

clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

If you haven't installed the command line tools for Xcode you can run the compiler and other tools without doing that by using the xcrun tool.

xcrun clang++ -std=c++11 -stdlib=libc++ -Weverything main.cpp

Also if there's a particular warning you want to disable you can pass additional flags to the compiler to do so. At the end of the warning messages it shows you the most specific flag that would enable the warning. To disable that warning you prepend no- to the warning name.

For example you probably don't want the c++98 compatibility warnings. At the end of those warnings it shows the flag -Wc++98-compat and to disable them you pass -Wno-c++98-compat.

Compile C++11 code on mac?

Outside of an IDE (e.g., in shell), I normally have the variable CXX set to: "clang -std=c++11 -stdlib=libc++" in .profile / .tcshrc / etc., since this is picked up by most configure scripts too. On the cmd line I might use: $CXX -c foo.cc

MacPorts gcc-4.8.1 works well: "[sudo] port install gcc48 [-universal]"

"[sudo] port select --set gcc gcc48" will make this the default gcc, g++, etc.

Don't attempt to update or modify the system tools, like the old gcc-4.2 / llvm hybrid that comes with Xcode.

I don't know what you mean by 'best' way in the 3rd part of your question, but with Apple's support (they employ the primary author of LLVM), and other projects like FreeBSD behind it, clang will only continue to improve. It's already much faster than gcc, has far better error messages / diagnostics (especially for C++ and templates), and a modular architecture. For OS X, it's the clear choice.

Making -std=c++11 the default in mac terminal

Create an alias: alias g++='g++ -std=c++11' should do the trick.

(However, the version of GCC that comes with OS X is so ancient that it doesn't support C++11, you'd be better off using clang and clang++.)

How can I run a C program on Mac OS X using Terminal?

First save your program as program.c.

Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How can you find App Store? Do a "Spotlight Search" by typing Space and start typing App Store and hit Enter when it guesses correctly.

App Store looks like this:

Sample Image

Xcode looks like this on App Store:

Sample Image

Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type Space and start typing Terminal and hit Enter when it guesses Terminal.

Now install the command-line tools like this:

xcode-select --install

Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:

gcc -Wall -o program program.c

Note: On newer versions of OS X, you would use clang instead of gcc, like this:

clang program.c -o program

Then you can run it with:

./program
Hello, World!

If your program is C++, you'll probably want to use one of these commands:

clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp

How can I compile and run C/C++ code in a Unix console or Mac terminal?

If it is a simple single-source program,

make foo

where the source file is foo.c, foo.cpp, etc., you don’t even need a makefile. Make has enough built-in rules to build your source file into an executable of the same name, minus the extension.

Running the executable just built is the same as running any program - but you will most often need to specify the path to the executable as the shell will only search what is in $PATH to find executables, and most often that does not include the current directory (.).

So to run the built executable foo:

./foo

How to compile and run C program on Mac OS X

You need to add a dot to indicate that the executable is in the current directory, as the current directory is not in the path:

./a.out

macOS: C++11 Compilation Error with Apple clang Version 13 In Terminal but not In Xcode

You need to add --std=c++11 to the g++ command line options in your Makefile. Visual Studio defaults to the latest possible C++ standard. gcc defaults to the earliest.



Related Topics



Leave a reply



Submit