在VSCode中用Emscripten编译C++代码时提示conio.h缺失,求解决方案
问题:Emscripten编译C++代码时提示'conio.h'未找到,且存在Web环境不兼容的控制台操作
错误输出
PS C:\Users\abc\emsdk> em++ f:/fighter/source.cpp -s WASM=1 -o f:/fighter/source.html f:/fighter/source.cpp:2:10: fatal error: 'conio.h' file not found #include "conio.h" ^~~~~~~~~ 1 error generated. em++: error: 'C:/Users/abc/emsdk/upstream/bin\clang++.exe -target wasm32-unknown-emscripten -fignore-exceptions -fvisibility=default -mllvm -combiner-global-alias-analysis=false -mllvm -enable-emscripten-sjlj -mllvm -disable-lsr -DEMSCRIPTEN -D__EMSCRIPTEN_major__=3 -D__EMSCRIPTEN_minor__=1 -D__EMSCRIPTEN_tiny__=18 -Werror=implicit-function-declaration -IC:\Users\abc\emsdk\upstream\emscripten\cache\sysroot\include\SDL --sysroot=C:\Users\abc\emsdk\upstream\emscripten\cache\sysroot -Xclang -iwithsysroot/include\compat f:/fighter/source.cpp -c -o C:\Users\abc\AppData\Local\Temp\emscripten_temp_px_3ahn7\source_0.o' failed (returned 1)
解决方案
1. 移除conio.h依赖
conio.h是Windows/DOS平台专属的非标准头文件,Emscripten(面向Web/WASM环境)不提供该头文件。检查代码后发现,你并没有使用任何conio.h中的函数(声明的char ch未被使用),直接删除代码中的#include "conio.h"即可解决编译错误。
2. 替换Windows专属的system命令
代码中使用的system("color")、system("pause")、system("cls")都是Windows控制台专属命令,在Web/WASM环境下完全无效,需要替换为Web兼容的实现:
颜色控制:用ANSI转义序列替代
system("color"):- 红色(对应
color 04):cout << "\033[31m"; - 绿色(对应
color 02):cout << "\033[32m"; - 重置颜色:
cout << "\033[0m";
- 红色(对应
暂停等待输入:用
cin.get()替代system("pause"),需先处理之前输入留下的换行符:cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "按任意键继续..."; cin.get();(需要引入
<limits>头文件使用numeric_limits)清屏:用ANSI转义序列
cout << "\033[2J\033[H";替代system("cls"),该序列会清空控制台并将光标移至左上角。
3. 修复随机数种子问题
代码在while (!GameOver)循环中每次调用srand(time(0)),会频繁重置随机数种子导致生成的随机数重复。应仅在程序开始时调用一次srand,比如在main函数开头。
修改后的完整代码示例
#include <iostream> #include <stdlib.h> #include <cstdlib> #include <ctime> #include <emscripten.h> #include <limits> // 用于cin.ignore的numeric_limits using namespace std; bool GameOver = false; EMSCRIPTEN_KEEPALIVE void Processing() { int choose; cout << "\033[31m"; // 替代system("color 04") cout << "WELCOME" << endl; for (int i = 1; i < 5; i++) { cout << "Character " << i << endl; } cout << "choose character number: "; cin >> choose; if (choose > 4) { cout << "Enter valid character number: "; cin >> choose; } cout << "Selected Character is " << choose << endl; // 替代system("pause") cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "按任意键继续..."; cin.get(); cout << "\033[2J\033[H"; // 替代system("cls") int random = (rand() % 4) + 1; cout << "\033[32m"; // 替代system("color 02") cout << "ENEMY IS CHARACTER " << random << endl; int hp1 = 100; int hp2 = 100; cout << "Character " << choose << "(health " << hp1 << ")" << " VS. Character " << random << "(health " << hp2 << ")" << endl << endl; while (!GameOver) { int rand1 = (rand() % 100) + 1; int rand2 = (rand() % 100) + 1; int NewHp1 = hp1 - rand1; int NewHp2 = hp2 - rand2; cout << "Press any key to attack" << endl << endl; if (NewHp2 <= 0) { cout << "Enemy has 0 hp left"<<endl<<"YOU WIN!!" << endl; GameOver = true; } else if (NewHp1 <= 0) { cout << "\033[31m"; // 替代system("color 04") cout << "You have 0 hp left"<<endl<<"YOU LOSE" << endl; GameOver = true; } else{ hp1 = NewHp1; hp2 = NewHp2; cout << "Enemy has " << NewHp2 << " health left." << endl << endl; // 替代system("pause") cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "按任意键继续..."; cin.get(); cout << "You have " << NewHp1 << " health left." << endl << endl; } } cout << "\033[0m"; // 重置颜色 } int main() { srand((unsigned)time(0)); // 仅初始化一次随机数种子 while (!GameOver) Processing(); return 0; }
编译注意事项
编译时添加-s NO_EXIT_RUNTIME=1参数,避免WASM运行时在程序结束后退出:
em++ f:/fighter/source.cpp -s WASM=1 -s NO_EXIT_RUNTIME=1 -o f:/fighter/source.html
内容的提问来源于stack exchange,提问作者Adarsh
相关产品推荐
相关产品推荐

