Exercise :)

Hi there

I am helping educational website with their C++,
Website runs scoring system, where you have to resolve C++ puzzles and person that does this should do it in minimal amount of code.

What I need to add to main program is following:

1) the compiler name
2) the compiler version
3) the latest version of C++ it supports.

I know they are using g++, but i don't have too much luck (yet) in finding way to check version etc.
so far my searches result in running in in console g++ -v
and one website that talks about macros.
http://nadeausoftware.com/articles/2012/10/c_c_tip_how_detect_compiler_name_and_version_using_compiler_predefined_macros

Anys suggestions are welcomed :)
yes, so this how can be done:

1
2
cout << "Newest standard supported : C++11 - "<< __cplusplus << endl;
printf("gcc version: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);
Last edited on
Hi xxvms, the cplusplus macro gives a year and date as output. How does one translate this to a version like gcc 6.1.1 etc?
http://coliru.stacked-crooked.com/a/dac9dbbd638476a1
thanks gunnerfunner :)
sorry i don't understood your joke, i asked you a question - do you have a reply?
I was looking for an answer and I was getting nowhere with Google. So I wrote my question, but i persisted with friend google and after some time I found what is the answer to my question.
I decided to (in a joking way) to answer my own question and leave it here so if someone else have similar request they might benefit from it :)
but thank you for your input as well :)

at some point this will be added to the website http://cyber-dojo.org


I think your question is (if I understand correctly) about %d.%d.%d ???
correct?
I am not sure, I tried with and without.
Output from the website with this line is like this:

Newest standard supported : C++11 - 201103
gcc version: 6.2.0


and code I have added is:
1
2
 cout << "Newest standard supported : C++11 - "<< __cplusplus << endl;
 printf("gcc version: %d.%d.%d\n",__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__);


when I tried without %d then I am not getting correct output. as this is more i think C like I was not investigating.
Last edited on
You can replace the second line with the following for a "C++" version.
cout << "gcc version: " << __GNUC__ << '.' << __GNUC_MINOR__ << '.' << __GNUC_PATCHLEVEL__ << '\n';

But note that g++ version 6.2.0 supports C++98, C++11, C++14, and has experimental support for parts of C++17.

And note (from the documentation for this version of the C++ compiler (g++))
The default mode for C++ is now -std=gnu++14 instead of -std=gnu++98.

Note that by default this version of the compiler supports gnu++14, which has several compiler hacks not supported by the standard. If you want a truly standard compliant compiler you must specify the following compile switches -std=C++14 -pedantic to tell the compiler not to use the hacks.

Last edited on
Hi Jlb

Thank you for this :)

I was mistaken, I found some references on google but it appears it was outdated or wrong :)

thank you for correcting me.

:)
all right guys :) thank you for that.
as soon as I have time I will try upgrade path. I hope I will not regret it.
The GNU compiler: compile C++ code with -std=c++14 -pedantic-errors
Both these options are required.

We can use <boost/config.hpp> to get human readable information about the implementation (compiler, standard library and platform).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <boost/config.hpp>
#include <string>

int main()
{
    constexpr long cpp_ver = __cplusplus ;

    if( cpp_ver == 201703L ) std::cout << "C++17\n" ;
    else if( cpp_ver >=  201402L ) std::cout << "C++14\n" ;
    else if( cpp_ver >=  201103L ) std::cout << "C++11\n" ;
    else if( cpp_ver >=  199711L ) std::cout << "C++98\n" ;
    else std::cout << "unknown C++ version\n" ;

    std::cout << "compiler: " << BOOST_COMPILER << '\n'
              << " library: " << BOOST_STDLIB << '\n'
              << "platform: " << BOOST_PLATFORM << '\n' ;
}

http://coliru.stacked-crooked.com/a/5f1afef26e6b2a43
wow thanks JLBorges
Topic archived. No new replies allowed.