2012-11-30 12 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

あなたは

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

あなたのリンクライブラリに-lrtを追加する必要があります更新(2016年8月31日)

これはまだ問題のようです。あなたがman clock_gettimeを検索すると、これは、溶液(-lrt)につながり、それはまた、(のみ2.17前のglibcのバージョン用)-lrtと

リンクを言います。

あなたのglibcが新しい場合、あなたの問題は別のものかもしれません。

+0

うわー。どうすればそれを知ることができましたか?みんな、ありがとう! –

8

Add -lrt g ++の呼び出しに - clock_gettimelibrt.soです。