Installing C++11 on Ubuntu 12.04 LTS

Installing C++11 on Ubuntu 12.04 LTS

I used to code in C.  In fact, I wrote a lot of C code for many years starting back in the 80’s and probably through to the mid 90’s.  Recently, I’ve become interested in high speed messaging and wanted to learn more about IP multicast.  As with all things code related, I find the best way to learn is to write some code.  So I installed Eclipse for C/C++ and the eclipse C development tools and jumped write in (pardon the pun).  I wrote a couple of programs, but then quickly ran into some issues, not big issues, but things to contend with if I wanted to write code the way I usually write code.

For example, I wanted a threaded echo server, with the receive and send threads sharing data via a queue.  This takes all of 5 minutes to do in Java.  Not so in C.  I was looking at using PTHREADS and BOOST.  I’ve had *really* bad experiences with BOOST in the past, and granted it was over 10 years ago, it was so painful that it still hurts.  I remember debugging code that was going into the Pacific Stock Exchange integrating Javelin’s FIX engine and NEON’s MQ Series Integrator and having nothing but problems and risking a deadline.  So I wasn’t to excited.

I had been reading about C++11 and purchased Stroustrup’s new book, “The C++ Programming Language,” Fourth Edition, which covers C++11 as well as his original work covered C++.  In the longish preamble before we actually get to code, Bjarne states that good C code looks like good C++ code.  He also stated that C++11 included things like threading, and all of the wholesome goodness of BOOST without BOOST.  I was intrigued.  The idea of utilizing native concurrency functionality and such exotic things as lambda’s in C++ was too much to pass up.

The first thing to do was to install C++11 on Ubuntu 12.04 LTS – my preferred environment.  This is what I did:

First, we need to add a C++ repository, this one has C++:

$ sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update

Then, we search for the latest and greatest gcc and g++:

$ sudo apt-cache search "g\+\+"

And I found:

g++-4.8 - GNU C++ compiler

So I installed:

$ sudo apt-get install gcc-4.8 g++-4.8

At this point, I had whatever I had before for gcc and g++, and I wanted to keep them. So by using alternatives, I was able to easily change from that already installed version and the latest and greatest by doing this:

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 60 --slave /usr/bin/g++ g++ /usr/bin/g++-4.6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40 --slave /usr/bin/g++ g++ /usr/bin/g++-4.8
sudo update-alternatives --config gcc

After installing that, I installed one more package:

sudo apt-get install systemtap

Eclipse requires this to build and run from within the IDE.

And that was it. I went and created a default Hello World C++ project in Eclipse using the Linux (vs CROSS) compiler settings, ran it, and received this:

#include
using namespace std;

int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;
}

Produced this:

!!!Hello World!!!

Which appears to mean that everything is working correctly.

I hope this was helpful, and thanks for reading!

你可能感兴趣的:(ubuntu,C++11)