Ubuntu12.04GCC4.7 enable C ++ 11

Source: Internet
Author: User
To use the templatealiases feature, GCC4.7. and do not want to compile the source code, so install it as follows. Sudoadd-apt-repositoryppa: Ubuntu-toolchain-r/testsudoapt-getupdatesudoapt-get

To use the template aliases feature, GCC4.7.

And do not want to compile the source code, so follow the method below to install.

  1. Sudo add-apt-repository ppa: Ubuntu-toolchain-r/test
  2. Sudo apt-get update
  3. Sudo apt-get install gcc-4.7
  4. Sudo apt-get install g ++-4.7

If multiple containers exist in the Ubuntu 12.04 system, you can configure them in the cmakelists.txt at the top of the cmakeproject:

  1. Cmake_minimum_required (VERSION 2.8.7)
  2. Set (CMAKE_C_COMPILER "/usr/bin/gcc-4.7 ")
  3. Set (CMAKE_CXX_COMPILER "/usr/bin/g ++-4.7 ")
  4. Project (sample)
  5. Add_subdirectory (src bin)
Then run cmake In the build directory. You can see:
  1. Chenshu @ chenshu-beijing :~ /Work/research/tree/normal_tree/build $ cmake ..
  2. -- The C compiler identification is GNU
  3. -- The CXX compiler identification is GNU
  4. -- Check for working C compiler:/usr/bin/gcc-4.7
  5. -- Check for working C compiler:/usr/bin/gcc-4.7 -- works
  6. -- Detecting C compiler ABI info
  7. -- Detecting C compiler ABI info-done
  8. -- Check for working CXX compiler:/usr/bin/g +++-4.7
  9. -- Check for working CXX compiler:/usr/bin/g ++-4.7 -- works
  10. -- Detecting CXX compiler ABI info
  11. -- Detecting CXX compiler ABI info-done
  12. -- Processing ing done
  13. -- Generating done
  14. -- Build files have been written to:/home/chenshu/work/research/tree/normal_tree/build

However, my method is not the best method recommended by CMake. There are three methods available for reference:

Http://www.cmake.org/Wiki/CMake_FAQ#Method_3_.28avoid.29:_use_set.28.29

  1. How do I use a different compiler?
  2. [Edit] Method 1: use environment variables
  3. For C and C ++, set the CC and CXX environment variables. this method is not guaranteed to work for all generators. (Specifically, if you are trying to set Xcode's GCC_VERSION, this method confuses Xcode .)
  4. For example:
  5. CC = gcc-4.2 CXX =/usr/bin/g + +-4.2 cmake-G "Your Generator" path/to/your/source
  6. [Edit] Method 2: use cmake-D
  7. Set the appropriate CMAKE_FOO_COMPILER variable (s) to a valid compiler name or full path on the command-line using cmake-D.
  8. For example:
  9. Cmake-G "Your Generator"-D CMAKE_C_COMPILER = gcc-4.2-D CMAKE_CXX_COMPILER = g ++-4.2 path/to/your/source
  10. [Edit] Method 3 (avoid): use set ()
  11. Set the appropriate CMAKE_FOO_COMPILER variable (s) to a valid compiler name or full path in a list file using set (). this must be done before any language is set (ie before any project () or enable_language () command ).
  12. For example:
  13. Set (CMAKE_C_COMPILER "gcc-4.2 ")
  14. Set (CMAKE_CXX_COMPILER "/usr/bin/g ++-4.2 ")
  15. Project ("YourProjectName ")
  16. [Edit]

C ++ 11 is disabled by default. Add this line to the src/CMakeLists.txt file:

  1. SET (CMAKE_CXX_FLAGS "-std = c ++ 11") # Add c ++ 11 functionality
Then run

Cmake ..

Make VERBOSE = 1

You will see that the compiled g ++ command is followed:

  1. /Usr/bin/g ++-4.7-std = c ++ 11

Now we have succeeded. Enjoy C ++ 11.

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.