The Get function has errors when compiling code using gcc/g++ under Linux

Source: Internet
Author: User
Tags deprecated

The following error occurred when compiling a code named Myfirst.cpp with a g++ in Linux today

myfirst.cpp:In function ' int main () ':
Myfirst.cpp:11:2: Warning: ' char* gets (char*) ' is deprecated (declared at/usr/include/stdio.h:638) [- Wdeprecated-declarations]
Gets (CC);
^
Myfirst.cpp:11:9: Warning: ' char* gets (char*) ' is deprecated (declared at/usr/include/stdio.h:638) [- Wdeprecated-declarations]
Gets (CC);
^
/tmp/ccohyvhf.o:in function ' main ':
Myfirst.cpp: (. text+0xad): warning:the ' gets ' function is dangerous and should not being used.
/tmp/ccohyvhf.o:in function ' __static_initialization_and_destruction_0 (int, int) ':
Myfirst.cpp: (. text+0x103): Undefined reference to ' std::ios_base::init::init () '
Myfirst.cpp: (. text+0x112): Undefined reference to ' std::ios_base::init::~init () '
Collect2:error:ld returned 1 exit status

Careful look at the error message that the Get function is dangerous, specific information please Baidu or Google, but I use Get function only to enter a string of characters, the system can not use, I wrote a

PS: The following operations are done in Linux terminal, the system version is Ubuntu 14.04 LTS

Open Terminal (ctrl+alt+t)

Touch cstdio//Create a file Cstdio, I'm using C + +

Vim cstdio//Edit Cstdio, and add the following content

#include <cstdio>
void Mygets (char *str)
{
int i=0;
scanf ("%c", &str[i]);
while (str[i++]!= ' \ n ')
{
scanf ("%c", &str[i]);
}
str[i-1]=0;//is the default string end in the character array, and the ASCII code for the 0
}

So your own library function is written, I do not know the standard library functions exactly what, but certainly not me, hey

When used, the header file location is added

#include "Cstdio"//is double quotation marks instead of angle brackets

Mygets (str); STR book character array name

You can use the Get function indirectly under Linux.

If you use the C-language notation,

filename of the Cstdio file

Switch

Stdio.h, put the file inside of the

#include <cstdio>

Switch

#include <stdio.h>

Starting address of this article

https://www.textarea.com/suppermary/linux-xia-shiyong-gcc-g-bianyi-daima-shi-gets-hanshu-you-cuowu-564/

The Get function has errors when compiling code using gcc/g++ under Linux

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.