Cache's blog

By Cache, history, 3 years ago, In English,

Hi all
I am using Kubuntu 16.04
I write code in c++ and compile it using g++ .I think the version of c++ is c++ 11.
whatever be the reason i cannot use auto,unordered map and some other things in my pc.
I have googled quite a bit about updating to c++ 14 or some more advanced version which supports above things atleast.But in vain,I couldn't find any thing helpful that solves my problem.

Can someone help me how to run program in c++14 in my system??
And also I see some experienced coders compile program using something different rather than just
g++ prog.cpp Can someone elaborate on this ???

BTW Happy New Year to all of you

 
 
 
 
  • Vote: I like it
  • +5
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

The required switch is -std=c++14. Without explicitly selecting the Standard, it will default to C++11 for GCC 6.x and C++03 for GCC 5.x and below (g++ -v could be used to check the compiler version). However, you definitely should enable compiler warnings to work with GCC.

I use this script for C++ coding. Actually, it doesn't have an option to compile with C++14, though you can change the defaults in src/ec.py:315.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    can u say what is that you get by running with above mentioned script better than normal g++ prog.cpp

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +3 Vote: I do not like it
      1. Less typing. ec a instead of g++ -std=c++14 -Wall -Wextra -pedantic -Wformat=2 -Wfloat-equal -Wlogical-op -Wredundant-decls -Wconversion -Wcast-qual -Wcast-align -Wuseless-cast -Wno-shadow -Wno-unused-result -Wno-unused-parameter -Wno-unused-local-typedefs -Wno-long-long -DLOCAL_PROJECT -g -DLOCAL_DEBUG -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC a.cpp -o a && ./a <a.in. And you can switch to release build by just adding -r.
      2. Does not recompile if the file has not changed. Really saves time.
      3. Automatically #includes all the needed stuff. Why is it better than <bits/stdc++.h>? Compilation time, again. Up to 3 times faster.
      • »
        »
        »
        »
        3 years ago, # ^ |
        Rev. 2   Vote: I like it +5 Vote: I do not like it
        1. Makefile
        2. Makefile again
        3. Precompiled headers

        Q: Why Makefile?

        A: 5 lines vs. 500+ lines with much more features

        Q: Why precompiled headers?

        A: I just think that it's better then hard-coding all the function/class names in smth like this.

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      use alias for the command for the corresponding shell

      for eg I use "g main.cpp" and it will compile main.cpp into program named "a"

      then I can just execute a "./a"

      alias g='g++ additional_parameters -o a'

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        You're right, ec once was a little Bash alias, and it has grown into five hundred-lines Python script for these years.

»
3 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Simply put, use g++ prog.cpp -std=c++14 instead of just g++ prog.cpp

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Depends on the compiler version. If you have GCC 6.x , the compiler defaults to GNU C++ 14 -std=gnu++14. You should have GCC 5 or above for C++14 support. Use -std=gnu++14 or std=c++14 to use the C++14 standard.

Use g++ prog.cpp -std=c++14 for compiling

»
19 months ago, # |
  Vote: I like it +11 Vote: I do not like it

Recently I spent lot of unnecessary time(worth in total of more than 7 hours in past 4 days) in some debugging related to some issues like uninitialized variables, accessing out of bounds of an array. And everything works fine on my system but gives error when submitted.

I believe that there will be flags which will be useful for avoiding such things. Can somebody give a brief on the flags they generally use when compiling.
PS: Please give a one liner of what each flag is responsible (if possible).

  • »
    »
    19 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Sanitizer flags are useful for this kind of bugs: -fsanitize=address,undefined finds most common cases of undefined behaviour and invalid memory access. You can read more here.

  • »
    »
    19 months ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    This is a great starting point. Try these and remove those which feel obnoxious.