0

I'm learning C++ using the cplusplus website tutorials. I'm using the following software:

  • CentOS 6.4
  • Code::Blocks 10.05
  • GCC 4.4.7

these are the latest versions available from yum. As I was following the tutorial, I noticed that a lot of what it was saying did not work for me. For example, I can't prefix strings with 'u', 'U', 'L', etc. Also, 'nullptr' is not recognized, it produces a compiler error. I looked around and noticed that these features were added in C++11. So of course I thought I must have an old version of C++.

According to the GCC documentation in section 2.2 of this page, "The default, if no C++ language dialect options are given, is -std=gnu++98".

So I figured I need to specify -std=gnu++0x. I tried putting that in my Code::Blocks compiler arguments but it changed nothing. Instead I checked the compiler flag "Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]". That also changed nothing.

Can somebody please tell me what I need to do to get Code::Blocks and GCC to use the current version of C++?

msknapp
  • 1,595
  • 7
  • 22
  • 39
  • 3
    Your version of `gcc` is old: https://www.gnu.org/software/gcc/releases.html. Look into updating it. – NPE Aug 09 '14 at 19:41
  • IIRC, `nullptr` is GCC 4.5, but `L"blah"` should work regardless of C++ standard. – chris Aug 09 '14 at 19:44
  • That standard is not upcoming since 2011. – Deduplicator Aug 09 '14 at 19:45
  • Check answers at http://superuser.com/questions/381160/how-to-install-gcc-4-7-x-4-8-x-on-centos – prajmus Aug 09 '14 at 19:49
  • "sudo yum update gcc" says No Packages marked for Update. Why does yum have such an old version? – msknapp Aug 09 '14 at 19:52
  • string foo = L"blah"; has this error: conversion from 'const wchar_t [5]' to non-scalar type 'std::string' requested. – msknapp Aug 09 '14 at 19:53
  • @msknapp, Well that's normal. `L"blah"` is a wide string. `std::string` holds narrow strings. – chris Aug 09 '14 at 19:54
  • the tutorial never mentioned that, it just said I can use these prefixes. In fact the tutorial is not even showing me types in many of its examples. I thought a c++ string could handle each. Can somebody show me a c++ tutorial that is actually decent? I don't like this cplusplus.com tutorial at all – msknapp Aug 09 '14 at 19:59
  • 3
    @msknapp "Why does yum have such an old version?" Because you're using a distro that focuses on stability: existing systems have an old version of GCC, there's a risk that upgrading will break those systems, so CentOS stays on an older version. –  Aug 09 '14 at 20:00
  • @msknapp, Just get a good [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – chris Aug 09 '14 at 20:01
  • I switched to ubuntu and am using g++ version 4.8.2 now, unfortunately it still does not recognize u"blah", it says error 'u' was not declared in this scope. What am I still missing? – msknapp Aug 10 '14 at 01:57
  • @msknapp What's the exact command you've used? GCC 4.8 does support that, in C++11 mode, so assuming you're still passing the correct `-std=` option, that should work. (I don't have 4.8.2 installed, but I do have 4.8.3 to test, and features like this don't get added on minor releases.) –  Aug 10 '14 at 09:22
  • g++ 4.8.2 is still not the last, but should support most (if not all) of c++11. Are you using `-std=c++11` or `-std=gnu++11` ? –  Aug 10 '14 at 13:00
  • I needed to go into Code::Blocks's compiler settings and check the box for -std=c++11. Also had to use const char16_t* as my type instead of string. So now it's 'const char16_t * s = u"blah";'. I am really starting to hate CentOS for lagging behind so much. Thanks for the help. – msknapp Aug 10 '14 at 16:28

1 Answers1

1

The canonical flag to enable C++11 features is -std=c++11, but most of the C++11 features should work out of the box anyways, thanks to GCC extensions. You should set the flag anyways, because it's bad practice to rely on extensions when you can just adopt the new standard.

However, your version of GCC is a bit outdated. You should grab the last stable version from your package manager, or from https://www.gnu.org/software/gcc/index.html

Alternatively, you can try other compilers, a lot of them support C++11. Clang(++) is a popular choice.

  • 1
    For CentOS 6, OP has the current compiler they ship. They tend to lag a lot (as noted, for stability) which is great unless you depend on newer features. – Joe Aug 10 '14 at 12:38
  • Sure, it's pretty good for servers or machines shared by thousands of users. But for a mono-user desktop computer, rolling-release is often a better choice. In both cases, nothing forbid the OP to manually install any version of any software he want from sources, including gcc. –  Aug 10 '14 at 12:59