teja349's blog

By teja349, history, 8 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

| Write comment?
»
8 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.

  • »
    »
    8 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

    • »
      »
      »
      8 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.
      • »
        »
        »
        »
        8 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.

    • »
      »
      »
      8 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'

      • »
        »
        »
        »
        8 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.

»
8 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

»
6 years 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).

  • »
    »
    6 years 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.

  • »
    »
    6 years 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.