linking to boost regex in gcc - c++

i am trying to compile my program which uses regex on linux. I built the boost library in the
libs/regex/build
by typing
make -fgcc.mak
which created a directory gcc which contains the following four files
boost_regex-gcc-1_35
boost_regex-gcc-d-1_35
libboost_regex-gcc-1_35.a
libboost_regex-gcc-d-1_35.a
Now I want to use regex from my program which is in some arbitrary directory.
I #included boost/regex.hpp
I got the error which stated that regex.hpp is not found. Then I gave the -I option in the g++ compiler. I didn't get that error.
But I get the following error
undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::construct_init(boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)'
I googled and found that I need to somehow link one of the above 4 libraries to my program. How can I do it. Which one should I link and why?

Either add libboost_regex-gcc-1_35.a to your list of object files in your link step or add -static -lboost_regex-gcc-1_35 to the same. Also be sure that you have an -I switch pointing to your boost includes directory in your compile step. If the libraries are outside the typical search path (/usr/lib on *nix), add that directory to your link command with -Wl,-L/path/to/boost/libs for g++ or simply -L/path/to/boost/libs on ld.

I also faced similar problems when using boost filesystem. Here's what I needed to do to get it to link statically.
Excerpt from My Original (problematic) Makefile:
LIBS = -static -lboost_filesystem
Solution:
LIBS = -Wl,-Bstatic -lboost_filesystem -lboost_system -Wl,-Bdynamic
You can view the complete Makefile from
http://code.google.com/p/neptuner/source/browse/codebase/trunk/stratego/uboat/Makefile
Needed to add boost_system to make it link properly. Direct addition/specification of libboost*.a created more problems. Note the -Bdynamic is present to prevent static link of standard libraries.

Related

Linking error in boost program_options

I'm trying to build a 3rd party C++ library from source, and it depends on Boost. In the very last step when building, I got errors like this:
[ 90%] Linking CXX executable Shannon_RNASeq_Cpp
CMakeFiles/Shannon_RNASeq_Cpp.dir/src/main.cpp.o: In function `command_line_for_find_rep(int, char**, Shannon_C_setting&,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >&, std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)':
/home/lambda/Shannon_Cpp_RNA_seq/src/main.cpp:320: undefined reference to `boost::program_options::options_description::options_description(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, unsigned int)'
I searched other questions on StackOverflow (e.g. Boost Program Options link error), and initially thought that it was caused by the different compilers used to build the non-header part of Boost, so I downloaded the source of Boost 1.68.0 and built it in my personal directory using the same compiler I used to build the 3rd party library (the compiler is gcc 8.2.0), and from the error message, I can tell that C++11 standard was used (you can see cxx11 in the error message), so it can't be the case that compiler not supporting C++11 is causing the trouble, as supposed previously (e.g. undefined reference to boost::program_options in xubuntu). To build Boost with a custom compiler (the default compiler of the server is terribly outdated), I followed the instructions here.
Some further search reveals that this is a linking problem. The 3rd party library must be built by CMake, and I followed the instruction in an answer to a previous question (How to link C++ program with Boost using CMake), by adding this line to CMakeLists.txt in the root directory of the 3rd party library:
target_link_libraries(Shannon_RNASeq_Cpp ${Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE})
However, the same problem persists. I checked the file CMakeCache.txt, and confirmed that the desired compiler is used and Boost_PROGRAM_OPTIONS_LIBRARY_RELEASE points to the libboost_program_options.so in the directory where I put Boost 1.68.0 (the server has other older versions of Boost). I then used make VERBOSE=1 to check the bash commands invoked while building the library. The command doesn't sound wrong; the Boost program_options library has been linked; here's the command from make VERBOSE=1:
/usr/bin/cmake3 -E cmake_link_script CMakeFiles/Shannon_RNASeq_Cpp.dir/link.txt --verbose=1
It's referring to something in link.txt, and here's what I found there:
/home/lambda/mylibs/bin/c++ -g -rdynamic CMakeFiles/Shannon_RNASeq_Cpp.dir/src/main.cpp.o
CMakeFiles/Shannon_RNASeq_Cpp.dir/src/run_tasks.cpp.o -o
Shannon_RNASeq_Cpp lib_shannon/libmulti_graph_handler.a
lib_shannon/libcontig_graph_handler.a lib_shannon/libprimary_lib.a
-lboost_program_options lib_shannon/libsparse_flow_handler.a
-lglpk lib_shannon/libseq_graph_handler.a -lpthread
-lboost_system -Wl,-Bstatic -lmetis -Wl,-Bdynamic -lboost_filesystem
-lboost_program_options
So yes, Boost program_options has been linked (-lboost_program_options). I wonder if this is something wrong with program_options, since all errors that occurred at this stage are from program_options; I also linked to 2 other Boost compiled libraries, namely filesystem and system, and they do not appear in any error message I saw here.
I solved this problem myself (see my answer below), but new to CMake (and my only experiences with C++ is Rcpp), I don't know why it worked. Can someone please explain why switching to a static library worked? Also, when CMake linked to the default version of Boost on the server, it also by default used the .sos rather than the .as, even though the .as are available. Is there any way to tell CMake to use the .as rather than the .sos? Are there other solutions?
What I did was to change all the .sos in CMakeCache.txt into .as, like I changed libboost_program_options.so into libboost_program_options.a, and this error went away. Then the command from make VERBOSE=1 became:
/home/lambda/mylibs/bin/c++ -g -rdynamic CMakeFiles/Shannon_RNASeq_Cpp.dir/src/main.cpp.o
CMakeFiles/Shannon_RNASeq_Cpp.dir/src/run_tasks.cpp.o
-o Shannon_RNASeq_Cpp lib_shannon/libmulti_graph_handler.a
lib_shannon/libcontig_graph_handler.a lib_shannon/libprimary_lib.a
-Wl,-Bstatic -lboost_program_options lib_shannon/libsparse_flow_handler.a
-Wl,-Bdynamic -lglpk lib_shannon/libseq_graph_handler.a -lpthread
-Wl,-Bstatic -lboost_system -lmetis -lboost_filesystem -lboost_program_options -Wl,-Bdynamic

OpenCV: undefined reference to `cv::imread when using cmake

I have to use OpenCV on two systems, both Linux. My setup works on the first system, but fails on the second. For both systems I have installed OpenCV in a custom location using:
cd myBuildDir
cmake -DWITH_VTK=OFF -DCMAKE_BUILD_TYPE=Release
-DCMAKE_INSTALL_PREFIX=~/Software/OpenCVLibs/opencv-3.1.0/release
~/Software/opencv-3.1.0
make && make install
and similar for debug. The directories are different. One is with my user on my laptop, the other with a different user on a multiuser system.
I then used the example given in the OpenCV documentation to test my setup:
http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html
To build the example using my custom OpenCV location I used
cmake -DCMAKE_BUILD_TYPE=Release -DOpenCV_DIR=~/Software/OpenCVLibs
/opencv-3.1.0/release/share/OpenCV exampleSourceDir
This works on the first system (gcc version 5.3.0) but fails on the second (gcc version 4.6.3). Don't think the gcc version matter here, but stating them for completeness. I think it might be related to that on the first system there is no OpenCV in the system path installed, while in the second system there is. However the build fails with:
/usr/bin/cmake -E cmake_link_script CMakeFiles/DisplayImage.dir/link.txt --verbose=1
/usr/bin/c++ -O3 -DNDEBUG CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o -o DisplayImage -rdynamic /home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videostab.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_superres.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_stitching.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_shape.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_photo.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_objdetect.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_calib3d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_features2d.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_ml.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_highgui.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_videoio.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgcodecs.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_flann.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_video.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_imgproc.so.3.1.0
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib/libopencv_core.so.3.1.0 -Wl,-rpath,
/home/me/Software/OpenCVLibs/opencv-3.1.0/release/lib
CMakeFiles/DisplayImage.dir/DisplayImage.cpp.o: In function `main':
DisplayImage.cpp:(.text.startup+0xb8): undefined reference to `cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1d7): undefined reference to `cv::namedWindow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
DisplayImage.cpp:(.text.startup+0x1f4): undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
DisplayImage.cpp:(.text.startup+0x223): undefined reference to `cv::imshow(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: ld returned 1 exit status
From this call, I figure that the libraries in the custom location should have been used. Why does it fail then? I ran opencv_test_imgcodecs on both machines, which works fine.
Any ideas how to fix my problem? Is it because of the other OpenCV installation on the second system?
Thank in advance for any help!
I could solve my problem by changing the CMakeList.txt. I know have
find_package( OpenCV HINTS "~/Software/OpenCVLibs/opencv-3.1.0/release/share/OpenCV/" )
in it. Before it was just
find_package( OpenCV Required )
Now I also do not need to specify OpenCV_DIR any longer and can build with:
cmake -DCMAKE_BUILD_TYPE=Release exampleSourceDir
make
Most of the time though I think this a more general CMake mistake.
I just got the same error:
undefined reference to 'cv::imshow(cv::String const&, cv::_InputArray const&)'
And the fix that worked for me was changing my CMakeLists.txt from:
add_executable(rekf
src/rekf/rekf_node.cpp
src/rekf/ros_rekf.cpp
src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${catkin_LIBRARIES})
To:
add_executable(rekf
src/rekf/rekf_node.cpp
src/rekf/ros_rekf.cpp
src/rekf/rekf.cpp)
add_dependencies(rekf ${catkin_EXPORTED_TARGETS})
target_link_libraries(rekf ${OpenCV_LIBS} ${catkin_LIBRARIES})

Boost:;program_options 1.49 - can't link with -lboost_program_options

I'm attempting my first use of Boost anything so I thought I'd start with program_options. I'm developing on a Raspberry Pi running Debian Wheezy. I started by "apt-get install libboost1.49-all" and everything seemed to install correctly. I can see .a and .so libraries in /usr/lib.
/usr/lib/libboost_program_options.a
/usr/lib/libboost_program_options-mt.a -> libboost_program_options.a
/usr/lib/libboost_program_options-mt.so -> libboost_program_options.so.1.49.0
/usr/lib/libboost_program_options.so -> libboost_program_options.so.1.49.0
/usr/lib/libboost_program_options.so.1.49.0
I can compile some example source I found here by using
g++ boost_program_options.cpp -c
but I cannot get anything to link. I've tried explicitly specifying the library path using no -l and got nothing but several pages of undefined reference errors. I tried another example code and got a compile problem that indicated to me that I wasn't using "g++ -std=c++0x" but that's not the problem either. I'm stuck. I've also tried
g++ -std=c++0x boostme.cpp -o boostme -L/usr/lib -lboost_program_options
I'm just banging my head against the wall at this point. Among the stackoverflow posts I've seen so far, I'm doing everything correctly. My head hurts. ;-)
Just some sample error messages below. Still poking around.
/tmp/ccTbmurt.o: In function `boost::program_options::error_with_option_name::~error_with_option_name()':
boostme.cpp:(.text._ZN5boost15program_options22error_with_option_nameD2Ev[_ZN5boost15program_options22error_with_option_nameD5Ev]+0x118): undefined reference to `vtable for boost::program_options::error_with_option_name'
/tmp/ccTbmurt.o: In function `boost::program_options::validation_error::validation_error(boost::program_options::validation_error::kind_t, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
boostme.cpp:(.text._ZN5boost15program_options16validation_errorC2ENS1_6kind_tERKSsS4_i[_ZN5boost15program_options16validation_errorC5ENS1_6kind_tERKSsS4_i]+0x30): undefined reference to `boost::program_options::validation_error::get_template(boost::program_options::validation_error::kind_t)'
Because the signature of that destructor really should be
~error_with_option_name() throw() {}
I'm going to look into my crystal ball and say that - maybe - somewhere you might have #define throw() or similar hidden in your codebase.
That, or you might have conflicting version of the header files in your include paths, which do not correspond to the version of the libraries found at link time

Errors with regex on Ubuntu

I've a problem with using regex on Ubuntu,on Qt platform(I tried on Code::Blocks,too). I'm writing console application code and I must use regex.
When I typed #include <regex>,It to me that error(I think it's the most important error,but there are many errors of regex):
/usr/include/c++/4.9/bits/c++0x_warning.h:32: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the \
^
And I thought that I should use Boost library. I installed Boost with:
sudo apt-get install libboost-all-dev
and typed(and I edited things,with writing boost:: in the beginning):
#include <boost/regex.hpp>
But,unfortunately,It caused many errors,Like that:
In function `bool boost::regex_search<__gnu_cxx::__normal_iterator<char const*, std::string>, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
I'm really puzzled what I should to do.
You did not include the error message produced by <boost/regex.hpp>. Regarding the regex header provided by the standard library, the error message says it all: You have to use at least C++11, to have access to that header. E.g:
g++ -std=c++11 -Wall -Wextra -Werror foobar.cpp

Can't get googletest running with ubuntu (linker errors)

I've built googletest using make under linux, the resulting files are libgtest.a libgtest_main.a.
I referenced the include files from my application and added the following lib dependencies (in the given order):
-lgtest
-lpthread
However I get the following two linker errors when I try to compile:
more undefined references to `testing::internal::EqFailure(char const*, char const*, testing::internal::String const&, testing::internal::String const&, bool)
undefined reference to `testing::internal::String::ShowCStringQuoted(char const*)
From what I've seen within the googletest source, the EqFailure function is directly implemented within gtest.cc. I don't understand why I should get a linker error here, the other definitions from googletest could obviously also be found (if I remove -lgtest, I get alot of more linker errors).
What am I missing? Thank you in advance.
Make sure you do not accidentally mix your own gtest and the one shipped with ubuntu.
I use cmake to build and got the same errors due to cmake opting for /usr/include over my own custom built version (which is what I linked with).