Xcode support of lambda functions - c++

I have a program coded in VS that I'm trying to port over to Xcode. There are several issues I have ran into including use of lambda functions. Since Xcode uses gcc 4.2 and thus doesn't support C++11, will I not be able to use any lambda functions?
If I want to work on the code from my laptop without rewriting much of the code, will I have to install gcc 4.6 and compile using the terminal?

You have few options:
Re-write your code to the C++ 2003 standard.
Install GCC that supports C++11 features being used in the code and not use Xcode (you may use other IDEs, for example QtCreator or Eclipse CDT).
Wait for Xcode that comes with LLVM C++ compiler that supports C++11 features.

Related

Do any compilers currently support C++20?

I purchased a book recently entitled beginning C++20. I was looking to begin learning c++ though I now realize that I can't find a compiler that can run the code in the book as I get an error since the compiler I'm using (xcode) does not support c++ 20. I'm wondering if there are any compilers that I can run on my mac that support c++20.
gcc version 8 and up supports some of C++20; you can try using that.
It should also be noted that Xcode isn't a compiler, but instead an IDE that should be using clang as the actual compiler. Clang also currently has support for some of the C++ 20 features. To use them the -std=c++20 flag will still be needed.
Here can you find the currently implemented feature support of the GCC compiler of the C++20 specification:
GCC Link
But you need to enable it in your console command or add this to your toolchain: "-std=c++20"

Is the LLVM 5.0 compiler equivalent to the GCC compiler?

I have to write my projects for class in ISO C++ or C++/CLI and while the professor explains how to accomplish this in Windows, it is very difficult to know how to setup the equivalent on my Mac. I am currently running xCode 5.0.2 and it seems to compile the sample applications with no problem using the LLVM 5.0 compiler. I've read that Apple no longer supports GCC compiler, so my question is are the two compilers equivalent? Will code that runs in GCC compiler also work in the LLVM 5.0 compiler?
There is no LLVM 5.0. The LLVM project is currently at version 3.4. The Apple LLVM/Clang variant shipped with XCode 5 may carry a version number that corresponds to the XCode version, but that is mildly misleading.
That said, yes, most stuff that works with GCC 4.2 (the last one that Apple shipped) will work just fine with LLVM/Clang.
C++/CLI, on the other hand, is a Microsoft-proprietary thing and you will not be able to use it on a Mac no matter what, and neither GCC nor Clang support it.
Will code that runs in GCC compiler also work in the LLVM 5.0
compiler?
If by "runs" you mean "compiles," then yes, assuming your code is Standard-compliant.
Weather or not your code is Standard-complliant depends, in part, on how well your professor is doing his job.
Mostly. Being compatible with GCC is one of Clang's primary goals (see http://clang.llvm.org/features.html#gcccompat ). That said, you can install gcc via MacPorts, http://www.macports.org/ if you really need it.

I'm having some trouble with C++11 in Xcode

I'm a Mac OS X Lion user who uses Xcode for C++, and it appears that no updates are available for Xcode. I do not appear to be able to compile C++11-exclusive code, but for whatever reason, I thought Apple had gotten C++11 pretty much implemented. And yes, I do have support for Command Line Tools.
Then again, that might just be me. If so, is there any sort of IDE that supports C++11, or any way to upgrade?
I use Xcode and set the following settings:
C++ language dialect: C++11 or GNU++11
C++ Standart Library: libc++ (LLVM C++ standart library with C++11 support)
Xcode version: 4.3.2
If you're using Xcode 4.3 there are several relevant project settings you need to use C++11 features. The first is to use the clang compiler. Something like
GCC_VERSION = com.apple.compilers.llvm.clang.1_0
in your .xcconfig will set it, or you can use the GUI.
Next, you need to tell LLVM which C++ standard to use:
CLANG_CXX_LANGUAGE_STANDARD = gnu++11
This will make language features like range based for, delegated constructors, etc. available.
Finally, if you want to use C++11 STL features (such as std::unordered_map) you need to use the libc++ STL:
CLANG_CXX_LIBRARY = libc++
On XCode 5 / opencv 2.4.9, I can select:
And it builds without error.
If i set the libc++ without specifying C++ Language Dialect then I get same issue as OP

How can I write cross-platform c++ programs on my mac?

My university professor has given a requirement for us in his c++ class: We must write our programs using the gnu C++ compiler (GCC). I understand that after installing xcode, I get an apple version of gcc. However, this is not fully cross compatible as I understand. So I have tried to install the gnu gcc compiler by using mac ports, but I have been unsuccessful. After doing:
port install gcc47
When I go to the terminal and run:
gcc47 -v
I get "command not found"
When I run:
gcc -v
I get:
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
Showing that I am still using apple's version. The part I am not understanding is:
How do I install gcc (the gnu version, not apple's version)
How do I use it with an ide, such as eclipse?
The version of GCC you use (Apple or gnu native) should be irrelevant for you to proceed.
Writing portable C++ has nothing to do with the compiler--outside of ensuring your compiler does it's best to adhere to the C++ standard. Luckily enough, GCC is one of the best C++ compilers out there.
You're not looking to do cross-platform C++ per-se, because true portable C++ is somewhat of a pain to write given the various state of C++ compilers for various systems (for example the CC on VAX/VMS doesn't support templates...). I suspect what you want to do is ensure the code you write on your mac, can be compiled by your prof. As long as GCC can handle it on your mac, it should be fine.
So to your specific questions:
Your first question is unnecessary, as you can use apple's supplied g++ to compile your code.
For your second question, I would highly recommend against using an IDE while trying to learn. IDE's offer some really great time saving features, but they hide some important aspects, that I believe are worth struggling with early in your learning process, and which will help strengthen your C++ skills. And while things have certainly have gotten better, some IDE's were notorious for creating non-portable C++ (ie using void main()).
If you are still set on using Eclipse, or XCode, since it doesn't matter which GCC installation you use, the default setup should work just fine.
I've run into troubles installing gcc47 via ports in the past, but gcc46 went as expected. You may also want to install gcc_select.
From there, you can use gcc_select, or specify the path explicitly in Eclipse's toolchain editor.
Other notes:
If you don't need C++11 features, then Apple's GCC 4.2.1 should work fine. Perhaps you can detail why you can't use it. Basically, Apple's added some extensions to the toolchain in some cases (marked APPLE_ONLY), and you have good control of what options are enabled/disabled.
Recent versions of Clang which ship with Xcode handle C++ well (including some support for C++11). There are some advanced things in GCC that I miss when using Clang, but Clang's current C++ support is really quite good.

C++11 compiler for windows

I was just watching a few videos on Channel9. I found things like lambdas really cool. when I tried to copy the example, it failed. auto didn't work either.
I'm using Nokia's qtcreator which ships with gcc-4.4.0.
I wanted to know which compiler has the fun features implemented so I could play around and learn. I'm not anti MS or anyhting so I don't mind trying Visual Studio if it has those features.
Nearly all C++11 features are implemented in the GCC 4.6.x . A good place to get latest GCC builds (MinGW) is the TDM website - http://tdm-gcc.tdragon.net/download . It should be easy to replace your current GCC with TDM GCC by overwriting files...
A special version of MinGW:
MinGW-Builds gives you everything gcc offers (currently 4.7.2)
That is: Including support for std::thread, std::async, std::future and friends.
As far as I know that's by far the most complete C++11 you can get on Windows.
You just get the MinGW-build binaries here. Unlike other gcc-based installations it supports posix threads, which are currently key to getting the gcc support for C++11 threads and friends working on Windows.
Extract the directory mingw to any location and add the following two paths to your PATH environment variable: (well, change F:\coding ...)
F:\coding\MinGW\bin
F:\coding\MinGW\msys\1.0\bin
Add both, separated by semi colon. You will need to log out or reboot. To verify that you got it right, you can open a command prompt, and write
g++ --version
You should get a response like this, mentioning MinGW-builds:
g++ (Built by MinGW-builds project) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc. ...
I wrote a more complete instruction for getting this going with Eclipse, here: http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds
For playing around and learning C++11 on Windows, I recommend MinGW Distro by Stephan T. Lavavej. The newest version contains GCC 4.8.2 and Boost 1.55.0.
Take a look at MinGW-w64 if you're looking for a gcc-compatible compiler on Windows that supports a number of C++11 features.
Also if you're just looking for lambas and auto, as well as some other C++11 features like decltype, etc., (again, not as many feature are implemented compared to the latest stable gcc branch) then you can also use the free Visual Studio 2010 Express for C++ on Windows.
See here for an overview of the compilers and the supported C++11 features.
Scott Meyers maintains a webpage here:
C++11FeatureAvailability
The First link on the Webpage is:
Apache Wiki Overview of C++11 Support in Several Compilers is what you should have a look at.It doccuments in detail C++11 features supported by all popular compilers.
The Visual Studio 11 preview also supports lambdas.
Visual Studio 2017 has support in C++11, C++14, & C++17. + some of the Modern C++ "experimental" modules. If you decide on Visual Studio, you set the C++ standard at the project properties. C++11 is by default, but you can set it to C++14, C++17, or latest draft.
As regards g++, C++0x feature support should be detailed here: C++0x/C++11 Support in GCC - GNU Project - Free Software Foundation (FSF)
If you want to test most of the C++11 syntax using a Windows machine you have two options:
Install Cygwin and compile from sources gcc-4.7 (latest snapshot) and clang++ with libstdc++. However it is not guaranteed that this will work.
Safest bet: Install a modern Linux (such as Ubuntu if you are a Linux newbie) in a virtual machine (VirtualBox is a free virtual machine application) and in this virtual machine compile gcc-4.7 and clang++. I was able to successfully compile both of them on Ubuntu 11.10 following the instructions from this website.
Best option, as of 2014, is to use Visual Studio 2013 updated with the latest CTP (this will work even for the Express edition).
Few Min-GW Compilers do not support C++ Version 11 or later. This version is required for thread in OpenCV. So I will suggest using TDM-GCC Compiler instead of MinGW Compiler. Install this compiler and set path C:\TDM-GCC-64\bin to the system's environmental variable.