連結(jié)時(shí)發(fā)生錯(cuò)誤。原因是找不到源代碼中使用的函數(shù)或全局變量。
錯(cuò)誤有多種原因和解決方案
函數(shù)名稱錯(cuò)誤
tset();
test(); // 拼寫正確
功能未定義
/* file.h */
extern void a(); // 生命
/* file.c */
void a() {} // 定義(必須定義)
頭文件不包括在內(nèi)
// 正確的引入頭文件
#include <sqlite3.h>
#include <zlib.h>
外部庫(kù)無(wú)法鏈接
clang -lsqlite3 hello.c
C和C ++文件混合
/* file.hpp */
#ifdef __cplusplus
extern "C" {
void foo();
void bar();
}
#endif
模板實(shí)體不存在
/* file.hpp */
template<class T> T max(T a, T b);
/* file.cpp */
template<class T> T max(T a, T b) { return a < b ? b : a; }