简体   繁体   English

链接boost库时出现另一个“未定义的引用”错误

[英]Another “undefined reference” error when linking boost libraries

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 $ 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()' usr / lib / gcc / i686-redhat-linux / 4.7.0 /../../../ libboost_timer.so:未定义引用`boost :: chrono :: steady_clock :: now()'
collect2: error: ld returned 1 exit status collect2:错误:ld返回1退出状态

If I add -lboost_chrono when I compile, I get this error: 如果我在编译时添加-lboost_chrono,我会收到此错误:

/usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_chrono.so: undefined reference to `clock_gettime' /usr/lib/gcc/i686-redhat-linux/4.7.0/../../../libboost_chrono.so:未定义对`clock_gettime'的引用
collect2: error: ld returned 1 exit status collect2:错误:ld返回1退出状态

I can trace clock_gettime to sys/time.h. 我可以将clock_gettime跟踪到sys / time.h。 Unfortunately, I cannot find a corresponding .so file to link to. 不幸的是,我找不到要链接的相应.so文件。 What am I missing here? 我在这里错过了什么?

You must add -lrt to your link libraries 您必须将-lrt添加到链接库中

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

Update (2016-08-31) 更新(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 当你查找man clock_gettime ,这会导致解决方案( -lrt ),但它也会说

Link with -lrt (only for glibc versions before 2.17). 链接-lrt(仅适用于2.17之前的glibc版本)。

So when your glibc is newer, your problem might be something else. 因此,当你的glibc更新时,你的问题可能就是其他问题。

-lrt添加到g ++调用中 - clock_gettime位于librt.so

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM