How to include/link boost::regex library? - c++

I am trying to use the boost regex library but I can't manage to compile my program. All related questions which involve similar error messages seem to relate to situations where regex library wasn't properly linked. I think that I am linking it, but maybe I am doing something stupid... Perhaps someone sees my mistake?
Here is my test program:
// ~/workspace/test/test.cpp
#include <string>
#include <iostream>
#include <boost/regex.hpp>
int main(){
std::string s("abaab");
boost::regex r("[ab]*");
std::cout << boost::regex_match(s,r) << std::endl;
}
Here are the boost binaries (I installed them via synaptic on Xubuntu):
/usr/lib/i386-linux-gnu$ ls libboost_regex* -al
-rw-r--r-- 1 root root 2169582 Okt 13 05:14 libboost_regex.a
lrwxrwxrwx 1 root root 24 Okt 13 05:13 libboost_regex.so -> libboost_regex.so.1.53.0
-rw-r--r-- 1 root root 1002568 Okt 13 05:14 libboost_regex.so.1.53.0
Nevertheless I'm getting quite an ugly error message when running
~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp
Here is the complete message:
~/workspace/test$ g++ -L/usr/lib/i386-linux-gnu/ -lboost_regex test.cpp
/tmp/cc1yDf0v.o: In function `bool boost::regex_match<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__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::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__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)':
test.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[_ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE]+0x4c): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match()'
/tmp/cc1yDf0v.o: In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)':
test.cpp:(.text._ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j]+0x22): undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'
/tmp/cc1yDf0v.o: In function `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::perl_matcher(__gnu_cxx::__normal_iterator<char const*, std::string>, __gnu_cxx::__normal_iterator<char const*, std::string>, boost::match_results<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__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, __gnu_cxx::__normal_iterator<char const*, std::string>)':
test.cpp:(.text._ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC2ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_[_ZN5boost9re_detail12perl_matcherIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS6_EEENS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEC5ES6_S6_RNS_13match_resultsIS6_S9_EERKNS_11basic_regexIcSD_EENS_15regex_constants12_match_flagsES6_]+0xcd): undefined reference to `boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::string>, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::string> > >, 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)'
collect2: error: ld returned 1 exit status
The header seems to be found, right? How else could gcc know about boost::re_detail::perl_matcher or boost::basic_regex?
I have no clue what the problem might be. Maybe it is the linking after all?
Thanks in advance!

You just needed to make sure the library comes after your own sources.
g++ -L/usr/lib/i386-linux-gnu/ test.cpp -lboost_regex
This is a FAQ:
Why does the order in which libraries are linked sometimes cause errors in GCC?
g++ linking order dependency when linking c code to c++ code
And probably quite a few more. I guess it's (sadly) one of those rites-of-passage that goes with the territory :(

Related

How to get Tesseract working with CGO on Windows?

I've been trying to get otiai10/gosseract (Go package for Tesseract C++ library) working on Windows without success. As a requirement, gosseract requires the installation of "tesseract-ocr, including library and headers".
The Tesseract Windows compiling documentation mentions the usage of SW and Vcpkg, which to my understanding relies on MSVC and is hence not compatible with Go.
The documentation also mentions the existence of Cygwin packages. I have tried leveraging them by installing tesseract-ocr (5.2.0-1), tesseract-ocr-devel (5.2.0-1), tesseract-ocr-eng (5.00-1) as well as mingw64-x86_64-gcc-g++ and mingw64-x86_64-gcc-core after which I defined the following environment variables:
CGO_CFLAGS=-IC:\cygwin64\usr\include
CGO_CPPFLAGS=-IC:\cygwin64\usr\include
CGO_LDFLAGS=-LC:\cygwin64\lib
PATH=C:\cygwin64\bin;...
CC=C:\cygwin64\bin\x86_64-w64-mingw32-gcc.exe
CXX=C:\cygwin64\bin\x86_64-w64-mingw32-g++.exe
However, trying to compile (through GoLand) an example project (otiai10/ocrserver) still results in linker errors:
GOROOT=C:\Users\REDACTED\go\go1.18rc1 #gosetup
GOPATH=C:\Users\REDACTED\go #gosetup
C:\Users\REDACTED\go\go1.18rc1\bin\go.exe build -o C:\Users\REDACTED\AppData\Local\Temp\GoLand\___go_build_github_com_otiai10_ocrserver.exe github.com/otiai10/ocrserver #gosetup
# github.com/otiai10/gosseract/v2
/usr/lib/gcc/x86_64-w64-mingw32/11/../../../../x86_64-w64-mingw32/bin/ld: $WORK\b133\_x003.o: in function `tesseract::TessBaseAPI::Init(char const*, char const*)':
/cygdrive/c/Users/REDACTED/go/pkg/mod/github.com/otiai10/gosseract/v2#v2.3.1/C:/cygwin64/usr/include/tesseract/baseapi.h:215: undefined reference to `tesseract::TessBaseAPI::Init(char const*, char const*, tesseract::OcrEngineMode, char**, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, bool)'
/usr/lib/gcc/x86_64-w64-mingw32/11/../../../../x86_64-w64-mingw32/bin/ld: /cygdrive/c/Users/REDACTED/go/pkg/mod/github.com/otiai10/gosseract/v2#v2.3.1/C:/cygwin64/usr/include/tesseract/baseapi.h:215: undefined reference to `tesseract::TessBaseAPI::Init(char const*, char const*, tesseract::OcrEngineMode, char**, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, bool)'
/usr/lib/gcc/x86_64-w64-mingw32/11/../../../../x86_64-w64-mingw32/bin/ld: /cygdrive/c/Users/REDACTED/go/pkg/mod/github.com/otiai10/gosseract/v2#v2.3.1/C:/cygwin64/usr/include/tesseract/baseapi.h:215: undefined reference to `tesseract::TessBaseAPI::Init(char const*, char const*, tesseract::OcrEngineMode, char**, int, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > const*, bool)'
collect2: error: ld returned 1 exit status
How can one get "tesseract-ocr, including library and headers" properly set up on a Windows machine for CGO compatibility?
> go version
go version go1.17 windows/amd64
> C:\cygwin64\bin\x86_64-w64-mingw32-gcc.exe --version
x86_64-w64-mingw32-gcc (GCC) 11.3.0
> C:\cygwin64\bin\x86_64-w64-mingw32-g++.exe --version
x86_64-w64-mingw32-g++ (GCC) 11.3.0
> C:\cygwin64\bin\x86_64-w64-mingw32-ld.exe --version
GNU ld (GNU Binutils) 2.38

CMake failing to link Boost regex library in Clion

I am trying to get Boost 1.72 release ready to use in my C++ project. OS is Windows 10. I use Clion as an IDE and CMake 3.17 and gcc 8.1.0 as toolchain. I tried to follow lots of different instruction on the web trying to "install" Boost properly, and I ended up with these steps (all italic folders names are valid full-qualified paths):
Unpack Boost source code, go to boost-sources-dir/tools/build, run bootstrap.bat gcc
Then run b2 install --prefix="provided-boost-build-folder"
Then add provided-boost-build-folder/bin to PATH variable.
Go back to boost-sources-dir and run b2 --build-dir="boost-sources-dir\build" --prefix="boost-install-dir" toolset=gcc install --build-type=complete -j 4
Now I have "include" and "lib" folders in boost-install-dir. I open Clion and add parameters for CMake in Clion settings:
-DBOOST_INCLUDEDIR="boost-install-dir\include"
-DBOOST_LIBRARYDIR="boost-install-dir\lib"
-DBOOST_ROOT="boost-install-dir"
And my CMakeLists.txt is below:
cmake_minimum_required(VERSION 3.14)
project(DBMSProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "--coverage")
find_package(Boost)
# I also tried to use the line below instead of a line above, but it gave me a strange error
# "Could NOT find Boost (missing: regex) (found version "1.72.0")"
# find_package(Boost REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(DBMSProject main.cpp /* some other stuff */)
target_link_libraries(DBMSProject -static)
#target_link_libraries(DBMSProject ${Boost_LIBRARIES}) # that didn't work
target_link_libraries(DBMSProject Boost::boost ${Boost_REGEX_LIBRARY}) # neither that does
Just some sample code in main.cpp to see if compiling and linking work:
/* Other headers */
#include <boost/regex.hpp>
. . .
int main() {
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
After trying to run this program, it gives lots of link errors:
[100%] Linking CXX executable DBMSProject.exe
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::cpp_regex_traits_char_layer<char>::cpp_regex_traits_char_layer(boost::re_detail_107200::cpp_regex_traits_base<char> const&)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/cpp_regex_traits.hpp:370: undefined reference to `boost::re_detail_107200::cpp_regex_traits_char_layer<char>::init()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::raw_storage::extend(unsigned long long)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/regex_raw_buffer.hpp:131: undefined reference to `boost::re_detail_107200::raw_storage::resize(unsigned long long)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::save_state_init::save_state_init(boost::re_detail_107200::saved_state**, boost::re_detail_107200::saved_state**)':
C:/Users//Douments/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:110: undefined reference to `boost::re_detail_107200::get_mem_block()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::save_state_init::~save_state_init()':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:118: undefined reference to `boost::re_detail_107200::put_mem_block(void*)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::match_imp()':
C:/Users//ocuments/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_common.hpp:221: undefined reference to `boost::re_detail_107200::verify_options(unsigned int, boost::regex_constants::_match_flags)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::unwind_extra_block(bool)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:1371: undefined reference to `boost::re_detail_107200::put_mem_block(void*)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `void boost::re_detail_107200::raise_error<boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > >(boost::regex_traits_wrapper<boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::error_type)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/pattern_except.hpp:75: undefined reference to `boost::re_detail_107200::raise_runtime_error(std::runtime_error const&)'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::perl_matcher<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<boost::sub_match<__gnu_cxx::__normal_iterator<char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::extend_stack()':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/perl_matcher_non_recursive.hpp:236: undefined reference to `boost::re_detail_107200::get_mem_block()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::basic_regex_parser<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::fail(boost::regex_constants::error_type, long long, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, long long)':
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:242: undefined reference to `boost::regex_error::raise() const'
C:/Users//Douments/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//v4/basic_regex_parser.hpp:241: undefined reference to `boost::regex_error::~regex_error()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_dtail_107200::basic_regex_creator<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::fixup_recursions(boost::re_detail_107200::re_syntax_base*)':
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:875: undefined reference to `boost::/Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:785: undefined reference to `boost::regex_error::~regex_error()'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:874: undefined reference to `boost::regex_error::~regex_error()'
CMakeFiles\DBMSProject.dir/objects.a(main.cpp.obj): In function `boost::re_detail_107200::basic_regex_creator<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::create_startmaps(boost::re_detail_107200::re_syntax_base*)':
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: undefined reference to `boost::regex_error::regex_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::regex_constants::error_type, long long)'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:941: undefined reference to `boost::regex_error::raise() const'
C:/Users//Documents/Programs/boost/include/boost-1_72/boost/regex/v4/basic_regex_creator.hpp:940: undefined reference to `boost::re
But I can clearly see with my eyes the definitions of "absent" symbols in the same .hpp file as where the errors are! What am I doing wrong?
I finally got it to work. My CMakeLists.txt now looks as follows:
cmake_minimum_required(VERSION 3.14)
project(DBMSProject)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "--coverage")
set(CMAKE_PREFIX_PATH ${BOOST_LIBRARYDIR}\\cmake)
find_package(Boost CONFIG REQUIRED COMPONENTS regex)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(DBMSProject main.cpp /* stuff */)
target_link_libraries(DBMSProject -static)
#target_link_libraries(DBMSProject ${Boost_LIBRARIES}) # haven't tried that but I think it might work as well
target_link_libraries(DBMSProject Boost::regex)
It also turned out that I don't need -DBOOST... parameters for CMake if I don't use them in CMakeLists.txt, so I excluded -DBOOST_INCLUDEDIR and -DBOOST_ROOT from them.

cannot compile boost::fiber official examples

I am trying out boost::fiber library, but I couldn't manage to compile code with boost fiber. Therefore I turned into compiling and running boost official examples. I installed latest version of boost library 1.65.1, and installation seems to be fine. I executed following command to compile simple.cpp
g++ -I /usr/local/include/boost/ -L /usr/local/lib/ -lboost_fiber -std=c++11 libs/fiber/examples/simple.cpp
But I get the following complains:
/tmp/ccWQ5ZMf.o: In function `main':
simple.cpp:(.text+0x7b): undefined reference to `boost::fibers::fiber::join()'
/tmp/ccWQ5ZMf.o: In function `boost::context::continuation::~continuation()':
simple.cpp:(.text._ZN5boost7context12continuationD2Ev[_ZN5boost7context12continuationD5Ev]+0x59): undefined reference to `ontop_fcontext'
/tmp/ccWQ5ZMf.o: In function `boost::context::continuation::resume()':
simple.cpp:(.text._ZN5boost7context12continuation6resumeEv[_ZN5boost7context12continuation6resumeEv]+0x68): undefined reference to `jump_fcontext'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::context::context(unsigned long, boost::fibers::type, boost::fibers::launch)':
simple.cpp:(.text._ZN5boost6fibers7contextC2EmNS0_4typeENS0_6launchE[_ZN5boost6fibers7contextC5EmNS0_4typeENS0_6launchE]+0x18): undefined reference to `vtable for boost::fibers::context'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::fiber::get_id() const':
simple.cpp:(.text._ZNK5boost6fibers5fiber6get_idEv[_ZNK5boost6fibers5fiber6get_idEv]+0x3b): undefined reference to `boost::fibers::context::get_id() const'
/tmp/ccWQ5ZMf.o: In function `boost::this_fiber::yield()':
simple.cpp:(.text._ZN5boost10this_fiber5yieldEv[_ZN5boost10this_fiber5yieldEv]+0x5): undefined reference to `boost::fibers::context::active()'
simple.cpp:(.text._ZN5boost10this_fiber5yieldEv[_ZN5boost10this_fiber5yieldEv]+0xd): undefined reference to `boost::fibers::context::yield()'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::fiber::fiber<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int, void, void, void>(void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int)':
simple.cpp:(.text._ZN5boost6fibers5fiberC2IRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEvvvEEOT_DpT0_[_ZN5boost6fibers5fiberC5IRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEvvvEEOT_DpT0_]+0x2c): undefined reference to `boost::context::stack_traits::default_size()'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::fiber::fiber<boost::context::basic_fixedsize_stack<boost::context::stack_traits>, void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>(boost::fibers::launch, std::allocator_arg_t, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int)':
simple.cpp:(.text._ZN5boost6fibers5fiberC2INS_7context21basic_fixedsize_stackINS3_12stack_traitsEEERFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEENS0_6launchESt15allocator_arg_tT_OT0_DpT1_[_ZN5boost6fibers5fiberC5INS_7context21basic_fixedsize_stackINS3_12stack_traitsEEERFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEENS0_6launchESt15allocator_arg_tT_OT0_DpT1_]+0x7f): undefined reference to `boost::fibers::fiber::start_()'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::worker_context<boost::context::basic_fixedsize_stack<boost::context::stack_traits> >(boost::fibers::launch, boost::context::preallocated const&, boost::context::basic_fixedsize_stack<boost::context::stack_traits> const&, void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int)':
simple.cpp:(.text._ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEC2INS_7context21basic_fixedsize_stackINSG_12stack_traitsEEEEENS0_6launchERKNSG_12preallocatedERKT_SB_SD_i[_ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEC5INS_7context21basic_fixedsize_stackINSG_12stack_traitsEEEEENS0_6launchERKNSG_12preallocatedERKT_SB_SD_i]+0x15d): undefined reference to `boost::fibers::context::~context()'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::run_(boost::context::continuation&&)':
simple.cpp:(.text._ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEE4run_EONS_7context12continuationE[_ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEE4run_EONS_7context12continuationE]+0xb5): undefined reference to `boost::fibers::context::terminate()'
/tmp/ccWQ5ZMf.o: In function `void* boost::context::detail::create_context2<boost::context::detail::record<boost::context::continuation, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<std::_Mem_fn<boost::context::continuation (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::*)(boost::context::continuation&&)> (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>*, std::_Placeholder<1>)> >, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<std::_Mem_fn<boost::context::continuation (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::*)(boost::context::continuation&&)> (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>*, std::_Placeholder<1>)> >(boost::context::preallocated, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<std::_Mem_fn<boost::context::continuation (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::*)(boost::context::continuation&&)> (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>*, std::_Placeholder<1>)>&&)':
simple.cpp:(.text._ZN5boost7context6detail15create_context2INS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEES7_SX_EEPvNS0_12preallocatedET0_OT1_[_ZN5boost7context6detail15create_context2INS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEES7_SX_EEPvNS0_12preallocatedET0_OT1_]+0xd8): undefined reference to `make_fcontext'
simple.cpp:(.text._ZN5boost7context6detail15create_context2INS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEES7_SX_EEPvNS0_12preallocatedET0_OT1_[_ZN5boost7context6detail15create_context2INS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEES7_SX_EEPvNS0_12preallocatedET0_OT1_]+0x10f): undefined reference to `jump_fcontext'
/tmp/ccWQ5ZMf.o: In function `void boost::context::detail::context_entry<boost::context::detail::record<boost::context::continuation, boost::context::basic_fixedsize_stack<boost::context::stack_traits>, std::_Bind<std::_Mem_fn<boost::context::continuation (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::*)(boost::context::continuation&&)> (boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>*, std::_Placeholder<1>)> > >(boost::context::detail::transfer_t)':
simple.cpp:(.text._ZN5boost7context6detail13context_entryINS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE[_ZN5boost7context6detail13context_entryINS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE]+0x70): undefined reference to `jump_fcontext'
simple.cpp:(.text._ZN5boost7context6detail13context_entryINS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE[_ZN5boost7context6detail13context_entryINS1_6recordINS0_12continuationENS0_21basic_fixedsize_stackINS0_12stack_traitsEEESt5_BindIFSt7_Mem_fnIMNS_6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEEFS4_OS4_EEPSO_St12_PlaceholderILi1EEEEEEEEvNS1_10transfer_tE]+0xc9): undefined reference to `ontop_fcontext'
/tmp/ccWQ5ZMf.o: In function `boost::fibers::worker_context<void (&)(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int), char const*, int>::~worker_context()':
simple.cpp:(.text._ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEED2Ev[_ZN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEED5Ev]+0x20): undefined reference to `boost::fibers::context::~context()'
/tmp/ccWQ5ZMf.o:(.rodata._ZTIN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEE[_ZTIN5boost6fibers14worker_contextIRFvRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEiEJPKciEEE]+0x10): undefined reference to `typeinfo for boost::fibers::context'
collect2: error: ld returned 1 exit status
Libraries (-l) should be specified after source/object files. The best place to specify -l is at the end of the command.
And boost_fiber depends upon boost_context.
So you'll need -lboost_fiber -lboost_context at the end of the command line.

Boost linking on Linux with GCC

I'm trying to compile a C++ program under Ubuntu 11.10 using boost 1.42 installed from the repository (I also tried building boost myself, but the result is the same as with the repo-boost). The source files compile but the linker gives errors... I tried for hours but couldn't find a solution to this, maybe someone can help me...
Here's the target from the Makefile
CXX = /usr/bin/g++
LDFLAGS = -L. \
-Lpath/to/libMy_Lib.a
CFLAGS = -I. \
-Wall \
-g \
-O0
OBJECTS = obj1.o obj2.o
%.o: %.cpp
$(CXX) -c $*.cpp -o $# \
-Wno-deprecated \
$(CFLAGS)
all: program
program: $(OBJECTS)
$(CXX) $^ \
$(LDFLAGS) \
-o myProg \
-lboost_regex \
-lboost_filesystem \
-lboost_date_time \
-lboost_system \
-lboost_thread \
-lMy_Lib
libMy_Lib.a is a library which also uses boost (I had no problems compiling it on the same system). All the libs look ok in /usr/lib...
Here is the output ld generates (I used make 2> output) http://ubuntuone.com/6QlU7AUZGgLGIu7sHbvDHm
Maybe the order of the libraries isn't correct (I know boost_filesystem depends on boost_system, but I'm not sure about the rest) or I forgot to specify some additional libs on which my program needs to link to...
This really buggs me and I feel like I'm blind to not see it...
Those error messages are impressive:
../../DIAG_DECODER//libDecoder_Element.a(BaseElements_Group.o): In function `bool boost::regex_match<__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> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__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::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)':
BaseElements_Group.cpp:(.text._ZN5boost11regex_matchIN9__gnu_cxx17__normal_iteratorIPKcSsEESaINS_9sub_matchIS5_EEEcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEEEbT_SD_RNS_13match_resultsISD_T0_EERKNS_11basic_regexIT1_T2_EENS_15regex_constants12_match_flagsE[bool boost::regex_match<__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> > > > >, char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::match_results<__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::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > > const&, boost::regex_constants::_match_flags)]+0x4c):
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> > >::match()'
I added a newline before the 'undefined reference'...
I think you should probably list your library, which uses Boost functions, before any of the Boost libraries.

libstdc++ - compiling failing because of tr1/regex

I have these packages installed on my OpenSUSE 11.3:
i | libstdc++45 | Standard shared library for C++ | package
i | libstdc++45-devel | Contains files and libraries for development | package
But when i'm trying to compile this C++ code:
#include <cstdio>
#include <tr1/regex>
using namespace std;
int main() {
int test[2];
const tr1::regex pattern(".*");
test[0] = 1;
if (tr1::regex_match("anything", pattern) == false) {
printf("Pattern does not match.\n");
}
return 0;
}
using
g++ -pedantic -g -O1 -o ./main.o ./main.cpp
It outputs this errors:
/tmp/cc0g3GUE.o: In function `basic_regex':
/usr/include/c++/4.5/tr1_impl/regex:771: undefined reference to `std::tr1::basic_regex<char, std::tr1::regex_traits<char> >::_M_compile()'
/tmp/cc0g3GUE.o: In function `bool std::tr1::regex_match<char const*, char, std::tr1::regex_traits<char> >(char const*, char const*, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)':
/usr/include/c++/4.5/tr1_impl/regex:2144: undefined reference to `bool std::tr1::regex_match<char const*, std::allocator<std::tr1::sub_match<char const*> >, char, std::tr1::regex_traits<char> >(char const*, char const*, std::tr1::match_results<char const*, std::allocator<std::tr1::sub_match<char const*> > >&, std::tr1::basic_regex<char, std::tr1::regex_traits<char> > const&, std::bitset<11u>)'
collect2: ld returned 1 exit status
What packages should i (un)install to make the code work on my PC?
You need to look at the c++0x library support for gcc as I think Regex is not yet completed
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.200x