简体   繁体   中英

Linking error Boost libraries

I built BOOST libraries with guides here: http://www.boost.org/doc/libs/1_65_1/more/getting_started/unix-variants.html Then I wanted to compile following code:

#include <boost/timer/timer.hpp>
#include <cmath>

int main()
{
  boost::timer::auto_cpu_timer t;

  for (long i = 0; i < 100000000; ++i)
    std::sqrt(123.456L); // burn some time

  return 0;
}

I issued a following command in a terminal

$ c++ -o program main.cpp -I /Users/miszo97/Desktop/boost_1_65_0 -L /Users/miszo97/Desktop/boost_1_65_0/stage/lib

then it yielded

Undefined symbols for architecture x86_64:
  "boost::timer::auto_cpu_timer::auto_cpu_timer(short)", referenced from:
      _main in main-a716e4.o
  "boost::timer::auto_cpu_timer::~auto_cpu_timer()", referenced from:
      _main in main-a716e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I guess something went wrong with linking. Maybe separately-compiled Boost libraries weren't build correctly?

There are two major options used for linking.

First there is the -L option which add a directory to search for libraries.

However, the linker don't know which libraries to link with automatically, it doesn't go though all possible libraries to find one that matches. And that's where the second option comes in: The -l (lower-case L) option which tells the linker to link with a specific library.

The libraries specified with -l are just the library names, not the full file names, and the linker uses the paths added with -L to find them.

In your case with the Boost timer library, the library name should be boost_timer , so you need to add the option -lboost_timer when linking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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