Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux C/C++ 開發常見編譯錯誤與解決方案速查手冊

編輯:Linux 教學

Linux 下 C/C++ 開發常遇到各類編譯、連結、執行錯誤。本文整理高頻錯誤訊息、根因與修正方法,適合開發者快速查閱。

Makefile 語法錯誤

錯誤訊息原因解決
Makefile:2: *** missing separator. Stop.指令行前缺少 Tab 鍵(空格無效)確保每條指令以單一 Tab 開頭
bash: ./Makefile: Permission denied直接執行 Makefile 文本檔使用 make 指令;非標準檔名用 make -f Makefile.am

原始碼字元編碼問題

錯誤訊息原因解決
stray '\200' in program
stray '\343' in program
原始碼混入中文標點(;,}、+)或全形空格關閉中文輸入法,刪除並重新輸入該行;或用 iconv 轉碼

缺少標頭檔導致的隱式宣告警告/錯誤

警告/錯誤缺少標頭檔補充指令
implicit declaration of 'printf'stdio.h#include <stdio.h>
implicit declaration of 'malloc'malloc.h 或 stdlib.h#include <stdlib.h>
implicit declaration of 'exit'stdlib.h#include <stdlib.h>
implicit declaration of 'execlp'unistd.h#include <unistd.h>
implicit declaration of 'strlen'string.h#include <string.h>
'FILE' undeclaredstdio.h#include <stdio.h>
'O_RDONLY' undeclaredfcntl.h#include <fcntl.h>
'CLONE_VM', 'CLONE_FILES' undeclaredsched.h#include <sched.h>
'pid_t' undeclaredsys/types.h(或 stdlib.h)#include <sys/types.h>

System V IPC semun 結構體未定義

錯誤: storage size of 'options' isn't known(union semun options;)
原因: /usr/include/linux/ipc.h 定義了 semun,但 POSIX 標準 sys/ipc.h、sys/sem.h 未定義。
解決: 自行定義:

union semun {
    int val;
    struct semid_ds *buf;
    unsigned short *array;
} arg;

字串長度與 sizeof 差異

  • C 風格字串以 '\0' 結尾。
  • sizeof(str) 包含結尾 '\0'。
  • strlen(str) 不包含 '\0'。

open() vs fopen() 差異速查

特性open()fopen()
標頭檔fcntl.h, sys/types.h, sys/stat.hstdio.h
回傳值檔案描述符FILE* 指標
層級系統呼叫標準庫緩衝 I/O
適用底層控制、非阻塞、鎖一般文字/二進位檔處理

除錯小技巧

  • 編譯加 -Wall -Wextra -Werror 將警告視為錯誤,強制修正。
  • 用 gcc -E 預處理輸出檢查巨集展開結果。
  • strace ./a.out 追蹤系統呼叫定位執行期錯誤。
Copyright © Windows教程網 All Rights Reserved