以下是在平常的實踐中出現的編譯錯誤
1、undefined reference問題總結
a、 鏈接時缺失了相關目標文件(.o)
b、 鏈接時缺少相關的庫文件(.a/.so)
c、鏈接的庫文件中又使用了另一個庫文件
同樣,如果我們的庫或者程序中引用了第三方庫(如pthread.a)則同樣在鏈接的時候需要給出第三方庫的路徑和庫文件,否則就會得到undefinedreference的錯誤。
d、多個庫文件鏈接順序問題
我們需要注意,在鏈接命令中給出所依賴的庫時,需要注意庫之間的依賴順序,依賴其他庫的庫一定要放到被依賴庫的前面,這樣才能真正避免undefinedreference的錯誤,完成編譯鏈接。
E、.h文件聲明函數,.cpp定義這些函數
包含.h文件,然後編譯
聲明和定義函數參數不一致
聲明:bool ceph_argparse_flag(std::vector<const char*>&args,
std::vector<constchar*>::iterator &i, ...)
定義:
bool ceph_argparse_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator i, ...)
錯誤點:
沒有加入&引用,注意要仔細對比檢查聲明和定義部分。
e、 在c++代碼中鏈接c語言的庫出錯
沒有使用extern “c”來標記
f、使用const對象訪問非const方法導致編譯器報錯
error:passing ‘const ECBackend::OverwriteInfo’ as ‘this’ argument of‘std::_Rb_tree_iterator<std::pair<const long unsigned int,std::pair<long unsigned int, long unsigned int> > >ECBackend::OverwriteInfo::end()’ discards qualifiers
g、默認參數,在定義中重複指定,只需要在聲明語句中指定
/usr/src/hj/PingAn/src/cluster/cluster_pool.cpp:1659:error: default argument given for parameter 3 of ‘intClusterPool::cc_list(Json::Value, int, std::string)’
/usr/src/hj/PingAn/src/cluster/cluster_pool.hpp:93:error: after previous specification in ‘int ClusterPool::cc_list(Json::Value,int, std::string)
h、error:static member function cannot have cv-qualifier
解答:A static member function shall not be declared const
, volatile
,
or constvolatile
.static
functions
haveno this
parameter. They need no cv-qualifiers.
i、error: invalid use of incomplete type ‘structJson::LogicError’
使用了只聲明沒有定義的類型
j、error:template with C linkage (c++程序被聲明爲c類型鏈接)
objclass/class_api.cc:In function 'int cls_cxx_map_set_vals(cls_method_context_t, conststd::map<std::basic_string<char>, ceph::buffer::list>*)':
objclass/class_api.cc:589:104:error: taking address of temporary [-fpermissive]
r =cls_cxx_setxattr(hctx, iter->first.c_str(),&(static_cast<bufferlist>(iter->second)));
這個並沒有解決問題,將bufferlist *轉換爲const bufferlist *是非法的操作
bufferlistin_bl(iter->second);
cls_cxx_setxattr(hctx,iter->first.c_str(), &in_bl);