2010-10-13 14:44

[C/C++語言] undefined reference to 錯誤排解

通常會出現 undefined reference to `function()' 這個錯誤有下面這兩個原因:
  1. 未連接正確的(靜態/動態)庫,或者是頭文件(*.h)和庫(*.a / *.so / *.dll)版本不匹配。
    g++ -o test *.o -L/MyProject/lib -lApiName

  2. 在 C++ 中引用 C 的函數時,有兩種作法:
    a.在 C 函數聲明(*.h)中用 extern C{…} 包起來。
    // file xx-api.h
    
    #ifndef XX_API_H
    #define XX_API_H
    
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void function_1(const char *srcpath);
    int function_2(void);
    
    #ifdef __cplusplus
    }
    #endif
    
    
    #endif
    

    b.或是在 C++ 將 #include 用 extern C{…} 匡起來。
    extern "C" {
        #include "yy-api.h"
    }
    


參考來源:
void value not ignored as it ought to be
我碰到过的编译和连接错误

0 回應: