运行C文件时出现main.exe不存在错误,添加date.c后触发
解决「Launch: program main.exe does not exist」问题及代码排查
核心原因:编译失败未生成可执行文件
添加date.c后,若编译时未将该文件纳入编译流程,会出现链接错误(找不到createDate、printDate的定义),导致无法生成main.exe,进而触发「program does not exist」错误。
解决步骤:
编译时包含所有源文件
若使用GCC编译,执行命令:gcc main.c date.c -o main.exe若使用VS Code,需配置
tasks.json确保编译任务包含两个源文件:{ "version": "2.0.0", "tasks": [ { "label": "compile", "type": "shell", "command": "gcc main.c date.c -o main.exe", "args": [], "group": { "kind": "build", "isDefault": true }, "problemMatcher": ["$gcc"] } ] }配置launch.json关联编译任务
空的launch.json需要指定程序路径,并添加preLaunchTask确保运行前先编译:{ "version": "0.2.0", "configurations": [ { "name": "C/C++: gcc.exe build and debug active file", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/main.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "gdb.exe", "preLaunchTask": "compile", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
代码问题排查(避免后续运行崩溃)
文件操作顺序错误
main.c中先调用fgets(myString, 1000, fp);再判断fp != NULL,若文件打开失败fp为NULL,会触发未定义行为。需调整顺序:fp = fopen("caches_all.csv", "r"); if (fp != NULL) { char myString[1000]; fgets(myString, 1000, fp); // 跳过表头 while (fgets(myString, 1000, fp)) { // 原有解析逻辑 } fclose(fp); } else { printf("Not able to open the file."); return 1; }printDate输出换行符date.c中printDate的printf包含\n,会导致main中输出内容被强制换行,若需连续输出可改为:void printDate(Date date) { printf("%04d/%02d/%02d", date.year, date.month, date.day); }CSV字段解析风险
使用%[^;]解析CSV字段时,若字段为空(如某列无内容)会导致后续解析全部错位,建议添加容错处理或使用更健壮的CSV解析逻辑。
内容的提问来源于stack exchange,提问作者André
相关产品推荐
相关产品推荐

