boost's regex won't compile - c++

I am using boost 1.45.0 on Ubuntu with Code::Blocks as my IDE, and I can't get basic_regex.hpp to compile. I'm pretty sure I set up boost correctly, because I can compile programs using boost::format without any errors. But I'm getting this annoying error, and I don't know how to get rid of it.
The code that is provoking the error:
boost::regex e("\"http:\\\\/\\\\/localhostr.com\\\\/files\\\\/.+?\"");
Compiler output (GCC):
obj/Debug/main.o
In function `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)'
/home/neal/Documents/boost_1_45_0/boost/regex/v4/basic_regex.hpp|379|
undefined reference to `boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::do_assign(char const*, char const*, unsigned int)'|
||=== Build finished: 1 errors, 0 warnings ===|
Did I miss a step when setting up boost, or should I downgrade to another version of boost?

This looks like a linker error. boost::regex is not a header only library so you need to pass -lboost_regex with correct -L/path/to/boost/lib to linker.

Boost::Regex has some code that lives in a separate library (libboost_regex.so). To link against it, add -lboost_regex to the GCC commandline you're using.
Depending on your install, that might be libboost_regex-mt.so. In that case, you'll need to use -lboost_regex-mt on your command line. (The MT stands for mutlithreaded.)

That's a linking error rather than a compiler error. You need to explicitly link against Boost's regex library.
g++ program.cpp -lboost_regex -L/path/to/boost/lib

Related

Codeblocks succeeded in compiling, but gcc failes, Why?

I'm using ubuntu 18.
I'v installed codeblocks ide and bare gnu gcc compiler (command line) both
gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)
code blocks 16.01
I wrote a very simple code to create and print a string variable
Code blocks(with inbuilt gnu gcc compiler) succeeded in compiling. But bare gcc compiler failes
I've tried by including iostream and stdio also
#include <string>
using namespace std;
int main() {
string a = "I am learning C++";
printf("%s",a.c_str());
return 0;
}
codeblocks compiles it successfully and gives the output without raising an error. As well as sololearn online compiler also
I am learning C++
I got into the folder where the cpp file is and gave following command
gcc string_var.cpp -o string_var.out
Bt the bare gcc compiler gives some long stuff of compiler errors which i can't understand
/tmp/ccen8bfp.o: In function `main':
string_var.cpp:(.text+0x20): undefined reference to `std::allocator<char>::allocator()'
string_var.cpp:(.text+0x37): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)'
string_var.cpp:(.text+0x43): undefined reference to `std::allocator<char>::~allocator()'
string_var.cpp:(.text+0x4f): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const'
string_var.cpp:(.text+0x74): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
string_var.cpp:(.text+0x96): undefined reference to `std::allocator<char>::~allocator()'
string_var.cpp:(.text+0xb0): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/tmp/ccen8bfp.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
The GNU Compiler Collection (GCC) can be used to compile many different languages. Some of those languages are common enough that they can use the same front-end program.
C and C++ are two such languages, and both can be compiled with the gcc front-end program. However, the gcc program by default is for C code, and because of that don't use some flags needed to build C++ programs. One such missing thing is that gcc doesn't link with the C++ standard library.
You can solve this by explicitly linking with the library:
gcc string_var.cpp -o string_var.out -lstdc++
Or, even simpler, use the front-end program for C++, g++:
g++ string_var.cpp -o string_var.out
gcc string_var.cpp -o string_var.out
gcc is a C compiler. You want to compile C++ code:
g++ string_var.cpp -o string_var.out
It just so happens that either gcc or g++ works insofar as compiling the actual code, the compiler understand that .cpp files contain C++ code, and proceeds accordingly.
However, when it times to linking the resulting object module, gcc does not provide the runtime C++ library to the linker (it still thinks it's just a C compiler, and has never heard of C++), resulting in your link failure.

undefined reference to boost::filesystem::path_traits::convert

I am trying to compile a program using cmake, and am seeing the following linker error:
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so:
undefined reference to
boost::filesystem::path_traits::convert(wchar_t const*, wchar_t
const*, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >&, std::codecvt<wchar_t, char, __mbstate_t>
const&)' /home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so:
undefined reference to
boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry
const&, std::__cxx11::basic_string,
std::allocator >&)'
The linker command that ninja generated looks like this:
g++ -pthread -DBOOST_ALL_DYN_LINK
utility/test/CMakeFiles/utilityTest.dir/loadCSVTests.cpp.o
utility/test/CMakeFiles/utilityTest.dir/main.cpp.o
utility/test/CMakeFiles/utilityTest.dir/randomDeviceTests.cpp.o -o
utility/test/utilityTest -rdynamic
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
utility/lib/libutilityLib.a utility/testLib/libutilityTestLib.a
utility/lib/libutilityLib.a
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
-Wl,-rpath,/home/quant/bin/boost_1_61_0/stage/lib
As you can see, I am linking against boost_filesystem and boost_system, so it's not the same problem as referenced on this SO post (and the many others like it).
I am using boost 1.61, which I compiled with gcc 5.3 (the same compiler as the one I'm compiling my program with).
What am I doing wrong?
I had a similar issue, this could be because of a new ABI which is introduced from gcc 5.1.
https://github.com/openframeworks/openFrameworks/issues/4203
I fixed mine by adding "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)" to CMakeLists.txt

undefined reference to `__stack_chk_fail'

Getting this error while compiling C++ code:
undefined reference to `__stack_chk_fail'
Options already tried:
added -fno-stack-protector while compiling - did not work, error persists
added a dummy implementation of void __stack_chk_fail(void) in my code. Still getting the same error.
Detailed Error:
/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getPar/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getParamInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: undefined reference to `__stack_chk_fail'
amInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: **undefined reference to `__stack_chk_fail'**
Earlier, I was getting 10's of such errors. Found out that there was a version mismatch between the gcc of the pre-compiled libraries I am using and the gcc version I was using to compile the code. Updated gcc and now I am getting only 2 of these errors.
Any help, please?
libgurobi_c++.a was compiled with -fno-stack-protector (obviously).
A few things come to mind:
add -fstack-protector when linking. This will make sure that libssp gets linked.
Manually link -lssp
Make your dummy version of __stack_chk_fail(void) in it's own object file and and add this .o file to your linker command AFTER libgurobi_c++.a. GCC/G++ resolves symbols from left to right during linking so despite your code having the function defined, a copy of an object containing the __stack_chk_fail symbol needs to be on the linker line to the right of libgurobi_c++.a.
In gentoo I had the same problem and i resolved creating 2 files. The first contain the option to be parsed by emerge and passed to gcc:
/etc/portage/env/nostackprotector.conf
CFLAGS="-fno-stack-protector -O2"
And the second tells which package should use this settings:
/etc/portage/package.env/nostackprotector
x11-libs/vte nostackprotector.conf
sys-libs/glibc nostackprotector.conf
www-client/chromium nostackprotector.conf
app-admin/sudo nostackprotector.conf
Just had the same issue: c++ code with an implementation of void __stack_chk_fail(void) showing several undefined reference to __stack_chk_fail errors when compiling.
My solution was to define __stack_chk_fail(void) as extern "C":
extern "C" {
__stack_chk_fail(void)
{
...
}
}
This suppressed the compilation error :)
Hope it helps!
https://wiki.ubuntu.com/ToolChain/CompilerFlags
says:
"Usually this is a result of calling ld instead of gcc during a build to perform linking"
This is what I encountered when modified the Makefile of libjpeg manually. Use gcc instead of ld solved the problem.

How can I use Boost::regex.hpp library in C++?

I tried to use Boost library but I failed, see my code:
#include "listy.h"
#include <boost/regex.hpp>
using namespace boost;
ListyCheck::ListyCheck() {
}
ListyCheck::~ListyCheck() {
}
bool ListyCheck::isValidItem(std::string &__item) {
regex e("(\\d{4}[- ]){3}\\d{4}");
return regex_match(__item, e);
}
When I tried to compile it I get those messages:
/usr/include/boost/regex/v4/regex_match.hpp:50:
undefined reference to
`boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator,
std::allocator > >,
std::allocator,
std::allocator > > > >,
boost::regex_traits >
::match()'
/usr/include/boost/regex/v4/basic_regex.hpp:425:
undefined reference to
`boost::basic_regex >
::do_assign(char const*, char const*, unsigned int)'
/usr/include/boost/regex/v4/perl_matcher.hpp:366:
undefined reference to
`boost::re_detail::perl_matcher<__gnu_cxx::__normal_iterator,
std::allocator > >,
std::allocator,
std::allocator > > > >,
boost::regex_traits >
::construct_init(boost::basic_regex > >
const&,
boost::regex_constants::_match_flags)'
etc...
You need to link to libboost_regex. Add -lboost_regex to the compiler switch if you're using gcc.
Those are linker errors. The Boost regex library is not a header-only library like shared_ptr (for example) - you need to link against the .a or .lib or whatever binary library.
You have to link against boost_regex.
had similar issue.
the solution was to link via cmake with the command target link library:
target_link_libraries(boostGraph Boost::regex Boost::date_time Boost::system Boost::filesystem Boost::thread Boost::graph Boost::program_options)
using the syntax -lboost_regex as offered here did not work (at least not with cmake)
the root issue could have been different versions of libraries, which cause the issue and even though the compiler and the linker do find the regex lib.

Boost Regex throwing an error

I have the following error when I try to compile my code in g+ compiler using eclipse
In function `ZSt19__iterator_categoryIPKSsENSt15iterator_traitsIT_E17iterator_categoryERKS3_':
C:/Program Files (x86)/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../../include/c++/3.4.5/bits/stl_algobase.h:(.text$_ZN5boost11basic_regexIcNS_12regex_traitsIcNS_16cpp_regex_traitsIcEEEEE6assignEPKcS7_j[boost::basic_regex<char, boost::regex_traits<char, boost::cpp_regex_traits<char> > >::assign(char const*, char const*, unsigned int)]+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)'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
All I have done is this statement
boost::regex re("\s+"); along with the header #inlucde
Could you kindly tell me how to proceed ?
It appears that you're not linking to the correct library. Most Boost libraries are header-only, so you don't need to do anything about them at link time. Boost::regex, however, is one of the few that requires that you link with a library along with giving the compiler the proper headers.
After you fix that, you'll want to re-check the escape in your string -- right now your passing "\s", which shouldn't be allowed (at a guess, you probably want "\s+" instead).