fatal error: cstdio: 没有那个文件或目录

没什么基础要自学真是挺多坑,不过填了一个坑就多一份经验。

今天在看pwn中关于GOT表的内容,其中需要编译一个程序,代码如下:

#include <cstdio>
#include <cstring>

using namespace std;

void win(){
        printf("you win\n");
}

int main(){
        char a[100];
        gets(a);
        printf("you lose\n");
        return 0;
}

原作者可能 没想到自己的文章受众基础太差,所以并没多作说明。于我保存为t1.c, 执行gcc t1.c进行编译,如果报错如是题所示:

t1.c:1:10: fatal error: cstdio: 没有那个文件或目录
 #include <cstdio>
          ^~~~~~~~
compilation terminated.

搜索了一下,基本上都是说未安装lib6-dev,或者此库文件损坏,按此思路操作一番,并没有解决问题。后来在一个国外网站得到一个信息,cstdio 是一个C++ 库,应该要将t1.c 改为 t1.cpp 再进行编译。想不到,gcc编译器是以后缀名来区别对待进行编译的。再次执行gcc t1.cpp,这次报另外的错误了:

t1.cpp: In function ‘int main()’:
t1.cpp:12:2: error: ‘gets’ was not declared in this scope
  gets(a);
  ^~~~
t1.cpp:12:2: note: suggested alternative: ‘fgets’
  gets(a);
  ^~~~
  fgets

继续搜索,得到比较多的信息是C++不再支持gets函数了,因为它不安全。那不能啊,我现在就是要用它来做例子,怎么办。继续搜索,这次在stackoverflow.com上得到有用信息:As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h,看来要换回C来编写

#include <string.h>
#include "stdio.h"

void win(){
        printf("you win\n");
}

int main(){
        char a[100];
        gets(a);
        printf("you lose\n");
        return 0;
}

保存为t1.c, 执行gcc t1.c,这次是警告,说明gets函数不安全,但是正常生成可执行文件了。

t1.c: In function ‘main’:
t1.c:13:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
  gets(a);
  ^~~~
  fgets
/tmp/cc4tcTYq.o:在函数‘main’中:
t1.c:(.text+0x37): 警告: the `gets' function is dangerous and should not be used.

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值