0

I am attempting to write a c++ program using the boost::sregex_iterator and receive a linking error when I attempt to compile. I have used boost multiprecision with success in the past, so I know that at least some boost libraries installed successfully. I am using clang (Apple LLVM version 6.1.0 (clang-602.0.49)) to compile on OS X 10.10.3.

The includes in my program are:

  #include <boost/multiprecision/gmp.hpp>
  #include <boost/regex.hpp>

and I am using the command line:

 c++ -I /usr/local/boost_1_57_0 input.cpp -lgmp -o words

The error message I get is quite long, but begins with:

 Undefined symbols for architecture x86_64:
 "boost::basic_regex...

I have read a number of other questions about people have similar problems, for example: How to link boost library 1.54 using clang?. From the responses, I gather that I should be linking -lboost_regex-mt, but when I add that to the command line I get the message: "ld: library not found."

My question is, if the problem is just that I need libboost_regex-mt, is there a way to install that without reinstalling all of boost? If I do need to reinstall boost, what should I do differently this time so that I end up with the regex library? If -lboost_regex-mt is not the problem, what do i need to do to be able to use boost regexes?

Community
  • 1
  • 1
Jonathan Basile
  • 649
  • 1
  • 10
  • 20
  • I figured it out - in case anyone comes across this in the future with a similar problem - there are certain boost libraries, including the regex library, which require installation steps beyond the normal boost installation. The install steps are detailed here: http://www.boost.org/doc/libs/1_58_0/more/getting_started/unix-variants.html#prepare-to-use-a-boost-library-binary - after that I used the command: c++ -I /usr/local/boost_1_57_0 input.cpp -lgmp -o words -L/usr/local/boost_1_57_0/stage/lib -lboost_regex – Jonathan Basile Apr 19 '15 at 20:11
  • I also had a further problem with the executable locating the library, which this solved: http://stackoverflow.com/questions/17703510/dyld-library-not-loaded-reason-image-not-loaded – Jonathan Basile Apr 19 '15 at 20:12

1 Answers1

1

This is answered here.

Presumably, if you installed boost correctly, you should have the libraries in addition to the headers. However, you're missing a "-L" option, which indicates where libboost_regex-mt.so lives. Without that option, the linker will just use its default search paths to find the library, and eventually complain if it can't be found.

Optionally, you can use the linker's rpath option embeds a library search path in the executable. This resolves the location of the .so when the executable is run. An alternative is to modify the LD_LIBRARY_PATH environment variable to include the location of the library.

So your example should look something like:

c++ -I /usr/local/boost_1_57_0 input.cpp -lgmp -L/usr/local/lib -Wl,-rpath,/usr/local/lib -lboost_regex-mt -o words

... assuming the library lives under /usr/local/lib.

Community
  • 1
  • 1
lhumongous
  • 1,064
  • 12
  • 27