------------新手必备:从编译错误到运行崩溃,一篇搞定
前言
Dev-C++ 是许多 C/C++ 初学者的入门 IDE,但其报错信息有时不够直观,容易让人一头雾水。本文整理了一些高频错误及其解决方法,希望能帮你节省调试时间。
错误速查表
| 序号 | 问题信息(报错内容) | 解决方案 |
| :---: | :--- | :--- |
| 1 | `'cout' was not declared in this scope` | 缺少头文件或命名空间。添加 `#include <iostream>` 并使用 `using namespace std;` 或写成 `std::cout`。 |
| 2 | `stray '\243' in program` | 代码中混入了中文空格、全角标点或不可见字符。用英文输入法重新输入代码,或将代码粘贴到纯文本编辑器(如 Notepad++)中检查。 |
| 3 | `Error 'for' loop initial declarations are only allowed in C99 or C11 mode` | Dev-C++ 默认使用 C89 标准,不允许在 for 循环内定义变量。解决方法:工具 → 编译选项 → 代码生成/优化 → 语言标准 → 选择 `ISO C99` 或 `GNU C99`。 |
| 4 | `undefined reference to 'winmain@16'` | 项目类型选错(创建了 Windows 应用程序而不是控制台程序)。新建项目时选择 `Console Application`,或者将源代码文件后缀改为 `.c` / `.cpp` 并重新编译。 |
| 5 | `Id returned 1 exit status` | 通常是上一个程序还在运行,导致链接失败。关闭之前运行的黑窗口,或者检查是否多个 main 函数、文件未保存。 |
| 6 | `cannot open output file ... Permission denied` | 可执行文件被占用。关掉所有 Dev-C++ 运行出的黑窗口,或者检查是否被杀毒软件拦截。 |
| 7 | `syntax error before 'return'` | 常见于遗漏分号、括号不匹配。检查上一行末尾是否有 `;`,以及大括号 `{}` 是否成对出现。 |
| 8 | `Warning deprecated conversion from string constant to 'char*'` | 将字符串字面量赋值给了 `char*`。C++11 后应使用 `const char*` 或 `string` 类型。 |
| 9 | `collect2.exe: error: ld returned 1 exit status` | 链接错误,通常由多重定义或未定义引用引起。检查是否有重复定义的函数/全局变量,或者头文件缺少 `#ifndef` 保护。 |
| 10 | `Segmentation fault`(运行时崩溃) | 常见原因:数组越界、空指针访问、栈溢出。使用 Dev-C++ 的调试功能设置断点,或添加 `printf` 定位出错位置。 |
|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 序号 | 问题 | 解决方案 |
| 1 | typedef int bool; #define true 1 #define false 0 4 13 D:\桌面\ModelFactory\Implement\Linear_sequentialList.h Error two or more data types in declaration specifiers | 使用已有BOOL三方库 #include <stdbool.h> |
| 2 | 13 2 D:\桌面\ModelFactory\Implement\Linear_sequentialList.h Error unknown type name 'SqList' | 定义基础类型 int/double/float/bool内存基址 |
| 3 | 11 23 D:\桌面\ModelFactory\Implement\Linear_sequentialList.h Error expected ';' before 'typedef' | C语言行末缺少;符号 |
| 4 | 11 3 D:\桌面\ModelFactory\Implement\Linear_sequentialList.c Error 'L' is a pointer; did you mean to use '->'? | 指针用->指向成员变量 |
| 5 | 7 D:\桌面\ModelFactory\main.c Error unterminated #ifdef | 既然#ifdef #elifdef #endif 不存在,那么用#ifdef #endif #ifdef #endif便行 |
| 6 | 9 2 D:\桌面\ModelFactory\main.c Warning implicit declaration of function 'log_info' -Wimplicit-function-declaration | log.h不能被隐式调用, #include "./Implement/log.h" |
| 7 | 27 86 D:\桌面\ModelFactory\Implement\Linear_sequentialList.c Error 'ElemTypes' undeclared (first use in this function); did you mean 'ElemType'? | ElemTypes拼写错误 |
| 8 | 编译窗口不见了 | 原文链接:https://blog.csdn.net/weixin_45846799/article/details/120685787 |
| 9 | 7 13 D:\桌面\ModelFactory\Implement\Linear_LinkList.h Error conflicting types for 'ElemType' | make clean再次执行编译 |
| 10 | 8 13 D:\桌面\ModelFactory\Implement\Linear_LinkList.h Error conflicting types for 'ElemType' | typedef float ElemType_float; typedef int ElemType_int; |
| 11 | D:\桌面\ModelFactory\collect2.exe Error ld returned 1 exit status C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe Implement/Linear_LinkList.o:Linear_LinkList.c:(.text+0x166): multiple definition of `PrintList'; Implement/Linear_List.o:Linear_List.c:(.text+0x268): first defined here | 改个名字 |
| 12 | 19 2 D:\桌面\ModelFactory\main.c Warning implicit declaration of function 'Print_LinkList'; did you mean 'PrintList'? -Wimplicit-function-declaration | 函数实现忘记更新头文件了 |
| 13 | 26 30 D:\桌面\ModelFactory\Implement\Linear_LinkList.c Warning implicit declaration of function 'malloc' -Wimplicit-function-declaration | 在C语言中,使用 malloc 需要包含 <stdlib.h> 头文件,否则编译器会假设 malloc 返回 int 类型(隐式声明),导致警告和潜在的错误。 |
| 14 | 指针使用 | C语言只支持值传递,函数内修改参数不影响外部,调用后外部指针仍为默认值。所有结果return见证。 |
| 15 | 14 2 D:\桌面\ModelFactory\include\Stack_LIFO.h Error unknown type name 'ElemType_Stak' typedef int ElemType_Stak; | 拼写错误 |
| 16 | 11 9 D:\桌面\ModelFactory\include\Stack_LIFO.h Warning ISO C99 requires whitespace after the macro name | #define STACKINCREMENT 10; //存储空间 分配增量 |
| 17 | 8 9 D:\桌面\ModelFactory\include\Stack_LIFO.c Warning implicit declaration of function 'LOG_ERROR' -Wimplicit-function-declaration | 大小写区分 |
| 18 | 如何获取文件组织 | CMD ls -R # 或者更美观的树形 tree /F >> /F 显示每个文件夹中的文件名,/A 使用 ASCII 字符而非图形字符。 POWERSHELL Get-ChildItem -Recurse | Select-Object FullName # 或者更美观的树形 tree.com /F |
| 19 | C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe Implement/?筛?新?罩?库?跋?有?罩?库?得?/log_auto.o:log_auto.c:(.text+0x24a): multiple definition of `log_set_level'; Implement/log.o:log.c:(.text+0x24a): first defined here | 中文路径,从project移除 |
通用排查步骤
-
**红色错误看第一行**:编译器报错往往一串,最上面的才是根本原因。
-
**双击报错行**:Dev-C++ 会跳转到大致位置,检查前后几行代码。
-
**确认编译器设置**:工具 → 编译选项 → 勾选"编译时加入以下命令" → 填入 `-std=c++11`(若需 C++11 特性)。
-
**清理并重新编译**:执行"工具 → 清除历史记录",再重新编译运行。
结语
遇到报错不用慌,大多数都是语法或配置问题。建议将本文提到的几种常见错误保存下来,下次遇到时直接对照排查。如果你有其他疑难报错,欢迎在评论区留言,我们一起完善这份列表。
*整理日期:Day0607 | 适用 Dev-C++ 5.11 及以上版本*