Another "undefined reference" error when linking boost libraries
Asked Answered
H

2

15

I've seen several other posts that deal with this exact same issue. However, none of their solutions seem to work for me. I am compiling the following code:

#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?

Howard answered 30/11, 2012 at 21:21 Comment(0)
C
23

You must add -lrt to your link libraries

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

Update (2016-08-31)

This still seems to be an issue. When you lookup man clock_gettime, this leads to the solution (-lrt), but it also says

Link with -lrt (only for glibc versions before 2.17).

So when your glibc is newer, your problem might be something else.

Cornett answered 30/11, 2012 at 21:26 Comment(1)
Wow. How would I have ever known to do that? Guys, thank you so much!Howard
A
9

Add -lrt to your g++ invocation – clock_gettime is in librt.so.

Allogamy answered 30/11, 2012 at 21:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.