How do I update C++ to C11 on Ubuntu 14.10?

Asked

Viewed 891 times

3

How can I upgrade the version of C++ to C11?

Details:

  • Ubuntu 14.10 32b
  • GCC 4.9.1 (Ubuntu 4.9.1-16ubuntu6)
  • Which Linux distribution do you use and which version? What problems are you having with upgrading?

  • Ubuntu 14.10 32b is giving an error when compiling a source and I informed myself that it is because of c++11 that needs to be installed and I am not getting :'( http://answall.com/questions/55915/error-itoa-was-not-declared-in-this-scope#55915

  • Thanks would be very helpful :D I am all day long behind solving this problem

  • 1

    I see no reason why you should want to close the question, "the question doesn’t seem about programming?..", just because there is code is not about programming? According to the scope this question fits into common tools among programmers". Frankly it is rather disappointing to close a question like this, which in my view is valid.

  • Here http://answall.com/questions/55915/error-itoa-was-not-declared-in-this-scope#comment115089_55928 he claims to be using version 4.7, which does not seem to be the case, that is, he may not even need to update. He can’t see what the version is right and can’t use the command he needs to run what he wants. He does not even know the difference between C++ and C, so the question is confused and leading to believe that he really wants to use C11, when not the case, he wants C++11. And if it’s in the version it says here, just use the -std=c++11 as I’ve shown him.

1 answer

4


In your specific situation there is nothing to be updated. As this your comment you use the version GCC 4.9.1, that supports the standard C11.

According to the GCC Wiki:

GCC 4.9 Changes: "ISO C11 support is now at a level similar to ISO C99 support.."

Basically you have to change the function itoa for std::to_string.

The real problem is using the function itoa, it is not a standard function and does not work on GCC on Linux (at least in my case). A minimum, complete and verifiable example of this problem may be:

#include <stdio.h>
#include <stdlib.h>

int main (){
  int i;
  char buffer [10];
  printf ("Digite um número: ");
  scanf ("%d", &i);
  itoa (i, buffer, 10);
  printf ("String: %s\n", buffer);
  return 0;
}

When compiling, the following message will appear:

$ g++ -std=c++11 main.cpp -o exemplo1
main.cpp: In function ‘int main()’:
main.cpp:9:22: error: ‘itoa’ was not declared in this scope
   itoa (i, buffer, 10);
                      ^
$

Note: If the above command returns the error to_string' is not a Member of Std, change -std=c++11 for -std=c++0x.

According to this article by IBM, the function std::to_string is a convenient way to implement what the function itoa makes. The same code, but for C++, can be made like this:

#include <iostream>
#include <string>

int main(){
    int numero;
    std::cout << "Digite um número: " << std::endl;
    std::cin >> numero;
    std::string str = std::to_string(numero);
    std::cout << "String " << str << std::endl;
}

If you use the Code::Blocks as IDE, and the message to_string' is not a Member of Std (or similar) appear to you, do the following:

  • Click on SettingsCompiler..

  • Navigate to the tab Compiler SettingsCompiler Flags

  • Check the option Have g++ follow the Coming C++0x ISO language standard -Std=c++0x.

    inserir a descrição da imagem aqui

Compile and run the code again. This may not work in specific situations, but it usually solves.


Updating

Ultimately the problem was solved using the function sprintf, which can be used as follows:

#include <string>
#include <iostream>

#define LIMITE 10

int main(){
    int numero = 1234567890;
    char str[LIMITE];
    sprintf(str, "%d", numero);
    std::cout << str << std::endl;
}

Note: It is not always recommended to use sprintf, in this specific case it is known beforehand the size of the variable numero, then there are no major problems in using it, but in certain situations where it is not known the size that a variable can assume (be it reading a file or user input), to avoid leaks use the function snprintf.

  • I am using linux Ubuntu 14.10 and build.sh and make and it appears that Std::to_string is not a member of the Std::! How to adapt this or solve this error in linux ?

  • -sh . /autogen.sh && . /configure --enable-server-diag --enable-mysql --enable-root-permission && make -j 2

  • @Aaronntoshinobi Do you compile using this syntax? If possible open the terminal and type the command: gedit autogen.sh copy everything and put it on http://www.pastebin.com, put the link here. Do the same also for the file configure: gedit configure copy everything and put on http://www.pastebin.com post the link here too.

  • autogen.sh : autoreconf -vfi --warnings=None ///////////////////////configure : http://pastebin.com/virTgxjF /////////////// build : http://pastebin.com/dMPsuksE

  • error: ? to_string' is not a Member of ːStd' /////////////// the error persisted

  • @Aaronntoshinobi Bah, this code you are trying to compile, can be made available? has as you uppar he somewhere and post the link here?

  • You have skype could inform me ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.