GNU GCC compiler updating - c++

I'm using code blocks with gnu gcc compiler. But when I try to compile a range base for startment it gives many errors. So I think the compiler dosn't support C++0x. So I want to know how to know the compiler version I'm using and how to update it correctly to a C++0x supporting one. I'm using Windows XP.

This algorithm may help to start with programming with C++11 on Windows:
1) Go to http://mingw.com and download the latest MinGW distribution,
install it
2) Download the CodeBlocks installer, install it. During installation
it should detect GCC installation and propose to use it as default
compiler, accept this.
3) Start a new CodeBlocks project, go to
Project -> Properties -> Build options -> Compiler settings ->
Compiler flags, then set flag "Have g++ follow the coming C++0x ISO
C++ language standard"
4) Compile your first C++11 project and have
fun!
By the way, the latest MinGW g++ is of version 4.6.1 as for now, so it has to support most of C++11 features.

Related

How can I use different Xcode versions for different branches (or latest Xcode with different Clang versions)?

We compile different branches of our project with different versions of Xcode, because it has different versions of the c++ compiler "clang" built-in. And a new compiler not only means new features, but unfortunately also new compiler bugs (hence new workarounds). So we want to keep the compiler once we branched a stable version of our project, but always use the latest compiler for the trunk.
Installing several Xcode versions is easy: We just add the Version number to the name and so we get:
/Applications/Xcode9
/Applications/Xcode10
/Applications/Xcode11
For the integration server and other scripts the required version can easily be picked and started with
xcode-select -s /Applications/Xcode10.app
xcodebuild -project myproj.xcodeproj build
But for working on the different branches with Xcode as graphical IDE this proves to be quite a problem:
Unlike Visual Studio Solutions (.sln and .vcxproj), Xcode Projects (.xcproj) don't support the specification of an IDE version they open in.
Different versions of Xcode don't play well together and seem to overwrite each others settings and caches (e.g. which projects/windows that were open, and even build status).
My last hope of solving this is to always use the latest Xcode IDE, but let it use an earlier compiler version.
There seems to be a project setting under Build Options for this, called Compiler for C/C++/Objective-C (GCC_VERSION when set via .xcconfig) with the standard value Default Compiler (Apple Clang) (com.apple.compilers.llvm.clang.1_0).
However, I was not able to find another valid value that I could use there. So far, I have tried clang version number and path to the clang executable (e.g. /Applications/Xcode10.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang). Xcode always says Unsupported Compiler.
So what is the correct way to specify another compiler version in Xcode? Or do you know another approach to solve the original problem?
I found a way to use an older clang version in latest IDE. This example works for using the Apple LLVM version 10.0.0 (clang-1000.11.45.2) in the Xcode 11.3:
In the .xcconfig file (Menu->File->New->File, "Configuration Settings File"), the compiler paths can be set explicitly with
CC = /Applications/Xcode10.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
CPLUSPLUS = /Applications/Xcode10.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang
But this is not enough: The system header files of a newer Xcode installation are sometimes incompatible with the old compiler. We need to set the roots for these files accordingly
OTHER_CFLAGS = -isysroot /Applications/Xcode10.app/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk

Getting a working C++11 toolchain with OSX & Eclipse without breaking something

I am currently starting to work seriously with C++. I've heard about the new features of C++11 and I like them. So I wonder whether I should write my new project according to the new standard. My current toolchain (that comes with XCode, I guess) does not support features like the auto keyword for type inference.
> g++
i686-apple-darwin11-llvm-g++-4.2
So I am looking for an easy and safe way to get a C++11 toolchain to try it out. I cannot risk breaking my old toolchain.
I know where to get binaries of GCC 4.8 for Mountain Lion, but I don't know how to install all the files manually (and would rather have a package manager do this for me). This discussion explains how to install GCC via homebrew, but I am affraid that this will overwrite and break my existing toolchain.
Also, I do not know how to configure a new toolchain in Eclipse after installation so I can use it with Eclipse/CDT.
You can use the homebrew package manager for OSX: Link
Have a look at https://github.com/mxcl/homebrew/wiki/Custom-GCC-and-cross-compilers and more specifically at homebrew dupes which has duplicates (but more recent versions) for software provided by OS X.
For a reasonable C++11 experience you should look for gcc 4.6 or gcc 4.7. When you have installed a recent version of gcc, you can then use it in your Makefiles. Mind you have to compile with -std=c++0x (gcc-4.6) or -std=c++11 (gcc-4.7+).
You can also look here How to enable C++11/C++0x support in Eclipse CDT? if you get syntax errors and warnings for C++11 constructs in Eclipse CDT.

Know g++ Version of Code blocks in Windows

I am solving questions on Interviewstreet.com. They said they use C++ version g++ 4.6.3,C0x mode.
I am writing code on code blocks. So i want to know which version iam using in code blocks is it in C0x mode or C11 mode??
I have tried using g++ --version i got g++ TDM-2 mingw32 4.4.1.Can u tell me where i can get this kind of information.
what is the difference between C++ 0x and C++11??
You'll have to update the version of g++ to 4.6.3 (or later) if you want to use c++11 features. See this question and it's answers on how to do it for deb linux.
Then you'll have to pass --std=c++0x to the compiler in options. You should be able to easily find them in codeblocks.
what is the difference between C++ 0x and C++11??
c++0x is a synonym for c++11.
The command:
g++ --version
gives you the version of your g++ or mingw compiler. Since you got g++ TDM-2 mingw32 4.4.1 then your version is 4.4.1. If you want to use version 4.6.3 as in that web site, then you would have to update.
It wouldn't hurt to use a newer than 4.6.3 version of mingw, so please see here for the latest version. This page offers an windows installer for mingw.
After installation, you would have to configure CodeBlocks to use the newly installed compiler by looking into Compiler and debugger settings -> Toolchain executables tab and setting the paths for the compiler-related executables to the new ones.
Hope this helps.
EDIT:
Here is a small tutorial/example of what the CodeBlocks settings look like.

How to update to C++11?

I am new to programming, so have never experienced a language update. With the release of C++11, I want to make use of all the new features such as lambda expressions and threads. I know I can do this with external libraries but using native language features would be more convenient.
I am using gcc 4.2.1 on Mac OS X Snowleopard in Xcode 3.2.6
What all do I need to do and update to start using C++11 features?
You can update to Xcode 4.1 (or whatever the most recent version you can get for Snow Leopard is) and get a new compiler with a few more C++11 features. There are some posts here on Stack Overflow about getting better support for C++11 in Xcode 4.1 on Snow Leopard.
But even the latest compiler available through Xcode does not support some C++11 features like lambdas. To get the best C++11 support you'll want to install a newer compiler, gcc 4.6 or 4.7, or Clang.
I frequently build the latest version of clang from source. It's not difficult to do if you're familiar with building other open source software. I use the git repos for clang and llvm, http://llvm.org/git/llvm.git and http://llvm.org/git/clang.git. You can also find instructions on their website for getting started: http://clang.llvm.org/get_started.html. Once you have the source for clang and llvm it's just ./configure && make && sudo make install. (you might want to run the tests before installing, since this is directly out of the repository. After make do make check in the llvm directory, and once that passes cd down to tools/clang and run make test. If everything is okay then sudo make install)
I don't remember if Snow Leopard included libc++ or not, so you may need to get that as well. http://libcxx.llvm.org/
Once everything is built and installed you can do:
clang++ -std=c++11 -stdlib=libc++ main.cpp && ./a.out
and you should have just about the best C++11 support around.
Recent patches in clang have really improved support for the last features you're likely to notice as a new C++ programmer. There are still a few bits and pieces left, but as of 3.1, and as far as I'm aware, clang has every C++11 feature that either gcc 4.7 or VC++11 has and more besides. libc++ also has the fewest gaps in terms of C++11 standard library features IME (though I think VC++'s standard library will also be pretty complete once they catch up on language features, e.g. char32_t and char16_t as native types so that the standard's mandated specializations for those types can be used).
Basically you only need to get a toolchain that has support for the new features. In macosx that would be either GCC or clang++. You might need to download/compile a particular version if needed (i.e. if the feature that you want to try is not in a prepacked compiler bundle but is available in the repository).
I downloaded and compiled the latest trunk of clang++ from subversion to do some testing, and installed g++ 4.6 with macports in Snow Leopard. I remember that I had some issues with the setup of the environment, but don't quite remember which of the compilers gave me problems or how I solved them. Google is your friend there :)
If you have homebrew installed, from this article, just two steps:
brew tap homebrew/dupes
brew install gcc --enable-cxx --enable-fortran --use-llvm

Searching for a C/C++ IDE for OS X that can use a custom gcc installation

I'm on OS X 10.7.2 "Lion", and I'm developing a C program that has to use some OpenMP 3.0 specific functionalities. Therefore, gcc 4.2.1 is not enough for me, I need gcc 4.4 or better.
I downloaded MacPorts and easily installed gcc 4.4. Running it from the command line just works fine, and compiles my OpenMP 3.0 C program with no hassle.
But it's quite annoying for me to edit the source in Xcode or TextWrangler and compile it from the command line. So I tried Code::Blocks, which took me 1 hour trying to make it work under OS X, but it crashes anytime for no reason. Then I tried Eclipse, but it sticks on using gcc 4.2.1 even if gcc 4.4 is installed and can be run from the command line. Finally, I tried MonoDevelop, which uses gcc 4.2.1 as well and I just can't see how can I force it to use my favourite gcc version.
What I'm kindly asking you is: is there any decent C/C++ IDE for OS X that I can use which will let me to specifiy what gcc version to use (just like Code::Blocks under Windows) without horrible workarounds and/or breaking gcc 4.2.1 (I need it up and working for Xcode)?
Thanks in advance.
You should be able to use your gcc with Xcode, without breaking the existing gcc toolchain. Follow these instructions (which describe how to use a custom clang build, but the same principle applies to gcc).
Eclipse is OK, it can be configured to use any compiler (or several at once or whatever).
it sticks on using gcc 4.2.1 even if gcc 4.4 is installed and can be run from the command line.
If you want a global setting, then go to C/C++ / Build / Environment in your preferences and set its PATH to wherever your favourite GCC is.
If you want to be more specific, you can edit a specific project's preferences, for instance:
C/C++ Build / Environment -> Environment variables to set (there you can find and customize the final PATH that your compilation will use, per build configuration)
C/C++ Build / Settings -> GCC C++ Compiler (and others) - there you can customise the exact binary file of the compiler to execute (defaults to just g++), again per build configuration.
This even lets you have a project with 2 build configs, one for GCC 4.2 and one for GCC 4.4 for instance.