2012-11-30 44 views
11

我見過其他幾個處理這個完全相同問題的帖子。但是,他們的解決方案似乎都不適合我。我在編譯以下代碼: 連接增強庫時出現另一個「未定義的引用」錯誤

#include <boost/numeric/ublas/matrix.hpp> 
#include <boost/numeric/ublas/io.hpp> 
#include <boost/timer/timer.hpp> 

using namespace boost::numeric::ublas; 

int main(){  
    matrix<double> mat1 (3,3); 
    matrix<double> mat2 (3,3); 
    matrix<double> mat3 (3,3); 

    unsigned k=0; 

    for(unsigned i = 0; i < mat1.size1(); ++i){ 
     for(unsigned j = 0; j < mat1.size2(); ++j){ 
     mat1(i,j) = k; 
     mat2(i,j) = 2*k++; 
     } 
    } 

    k=0; 
    if(1){ 
     boost::timer::auto_cpu_timer t; 
     while(k<1000){ 
     mat3 = prod(mat1,mat2); 
     k++; 
     } 
    } 
    return 0; 
} 

I am compiling from the command line using:

$ g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer

and receiving the following error:

usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_timer.so: undefined reference to `boost::chrono::steady_clock::now()'
collect2: error: ld returned 1 exit status

If I add -lboost_chrono when I compile, I get this error:

/usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_chrono.so: undefined reference to `clock_gettime'
collect2: error: ld returned 1 exit status

I can trace clock_gettime to sys/time.h. Unfortunately, I cannot find a corresponding .so file to link to. What am I missing here?

回答

18

您必須添加-lrt到您的鏈接庫

g++ matrix_test.cpp -o matrix_test -lboost_system -lboost_timer -lboost_chrono -lrt 

更新(2016年8月31日)

這似乎仍然是一個問題。當你查找man clock_gettime,這導致溶液(-lrt),但它也說

鏈接與-lrt(僅適用於2.17以前的glibc版本)。

所以當你的glibc更新時,你的問題可能是別的。

+0

哇。我怎麼會知道這樣做?夥計們,非常感謝你! –

8

Add -lrt到您的g ++調用 - clock_gettimelibrt.so

相關問題