Errors with regex on Ubuntu - c++

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

Related

LTO causes crash in standard library

Consider the following program:
#include <iostream>
#include <string>
int main()
{
std::string s;
std::getline(std::cin, s);
return 0;
}
I try to build it with various flags and run as echo foo | ./prog.
If I build it with clang 5.0 or gcc 7.1 (or 7.2) with optimization from -O0 to -O3, it works as expected. But if I add -flto to any of these configurations, it crashes immediately with the following backtrace:
/lib64/libc.so.6(+0x721af)[0x7f596b08e1af]
/lib64/libc.so.6(+0x77706)[0x7f596b093706]
/lib64/libc.so.6(+0x78453)[0x7f596b094453]
/usr/lib64/libstdc++.so.6(_ZNSs7reserveEm+0x85)[0x7f596b9ac055]
/usr/lib64/libstdc++.so.6(_ZSt7getlineIcSt11char_traitsIcESaIcEERSt13basic_istreamIT_T0_ES7_RSbIS4_S5_T1_ES4_+0x175)[0x7f596b984c05]
./a.out[0x400d7d]
./a.out[0x400c32]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7f596b03c6e5]
./a.out[0x400ab9]
Valgrind reports the same in a slightly more readable way:
==30863== Invalid free() / delete / delete[] / realloc()
==30863== at 0x4C2A8DC: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==30863== by 0x4F0E054: std::string::reserve(unsigned long) (in /usr/lib64/libstdc++.so.6.0.24)
==30863== by 0x4EE6C04: std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, char) (in /usr/lib64/libstdc++.so.6.0.24)
==30863== by 0x40091B: main (in /path/to/prog)
==30863== Address 0x6011c0 is 0 bytes inside data symbol "_ZNSs4_Rep20_S_empty_rep_storageE"
Also it works OK with --std=c++14 and below even with LTO enabled.
So what is the problem? Is it a bug in LTO implementation for C++17 in both compilers? Or just the libstdc++ is compiled with wrong flags? I use opensuse 42.3 and the standard library is installed from repositories.
Can it be worked around somehow?
For gcc this looks like this bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82172.
There are a number of workarounds, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82172#c3. One of them is to use -D_GLIBCXX_USE_CXX11_ABI=1:
g++ -D_GLIBCXX_USE_CXX11_ABI=1 --std=c++17 -flto prog.cpp
See also the same problem here flto crash with gcc7.2.
Following ks1322's links, I finally landed to binutils bug page, which
describes the root of the problem.
The bug was finally fixed in binutils 2.30 and backported to binutils 2.29 and 2.28. Updating to binutils 2.29 fixed the test example, but my actual build was still affected.
I figured out that one of my project's dependencies (I use CMake and external project module) was build without LTO. Adding -flto to all dependencies and using appropriate archiver (i.e. gcc-ar instead of ar) solved the problem completely.

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

icu 54 - undefined reference

Everything worked well with boost1.53, but when with boost 1.46, the project won't compile. I do understand there is no dependency between boost1.46 and icu, but it's weird that the same code could work normally with a higher version boost which relies on icu.
Invoking: GCC C++ Linker
g++ -o "CPPTest" ./worker/statistical_based_extraction.o ./common/utf8/icu_utf8_sequence.o ./common/tokenizer/indo_european_tokenizer.o ./common/features/indo_european_token_shape_feature_extractor.o ./common/crfpp_tagger.o ./crftest.o -lcrfpp -licuuc -lpython2.7
./worker/statistical_based_extraction.o: In function `GetLocale':
/home/tilney/workspace/CPPTest/Debug/../worker/statistical_based_extraction.h:25: undefined reference to `icu_54::Locale::Locale(char const*, char const*, char const*, char const*)'
/home/tilney/workspace/CPPTest/Debug/../worker/statistical_based_extraction.h:25: undefined reference to `icu_54::Locale::~Locale()'
Tried to configure icu with --disable-renaming, no luck.
tilney#tilney:/apps/nlp$ ls /usr/local/lib/libicu
libicudata.so libicui18n.so libicuio.so libicule.so libiculx.so libicutest.so libicutu.so libicuuc.so
libicudata.so.54 libicui18n.so.54 libicuio.so.54 libicule.so.54 libiculx.so.54 libicutest.so.54 libicutu.so.54 libicuuc.so.54
libicudata.so.54.1 libicui18n.so.54.1 libicuio.so.54.1 libicule.so.54.1 libiculx.so.54.1 libicutest.so.54.1 libicutu.so.54.1 libicuuc.so.54.1
Although Boost 1.46 doesn't provide Boost.Locale api, it DOES RELY on icu48, when you use apt-get to install boost, the corresponding icu would also be installed, which conflicts with the hand-installed icu. As for higher version boost, it was hand-installed so it didn't install any additional package.

How to link in std C++ library on Mac OS X Mavericks?

I'm porting an application to OS X Darwin and am getting link errors with missing symbols like:
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> >::find_last_of(char const*,
unsigned long,
unsigned long) const
operator delete[](void*)
typeinfo for std::runtime_error
std::set_unexpected(void (*)())
std::exception::~exception()
[...]
I expect these should come from libstdc++ but I don't see how to link that in using clang.
Here is my attempted link line and the resulting failure:
clang -std=c++11 -stdlib=libc++ -m64 -o ARCH.darwin_1310_i86/release/myExec ARCH.darwin_1310_i86/release/myExec.o ../../src/netcomm/ARCH.darwin_1310_i86/release/libmyExec.a ../../src/common/ARCH.darwin_1310_i86/release/libcommon.a -L../zlib -lz -L../Botan -lbotan-1.10 -lboost_thread-mt
Undefined symbols for architecture x86_64:
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::find_last_of(char const*, unsigned long, unsigned long) const", referenced from:
[...]
But this did not work, I'm not finding any examples of how to link it in correctly.
You need to add -lc++ to the link line like this:
clang -std=c++11 -stdlib=libc++ -lc++ -m64 -o ARCH.darwin_1310_i86/release/myExec ARCH.darwin_1310_i86/release/myExec.o ../../src/netcomm/ARCH.darwin_1310_i86/release/libmyExec.a ../../src/common/ARCH.darwin_1310_i86/release/libcommon.a -L../zlib -lz -L../Botan -lbotan-1.10 -lboost_thread-mt
After adding that, the missing symbols go away.
Using the CLang++ compiler on my MacBook Pro OS X Mavericks 9.2, within NetBeans 7.4, I have
-std=c++11 -stdlib=libc++ -Wall
I am sure that I obtained the libc++ from installing the latest Xcode Command Line Tools for Mavericks. On my system, the dynamic libc++ libraries are located in the /usr/lib directory.
Start Edit
I have just tried a basic, "Hello, World," run from Xcode, making sure the LLVM 5.0 (CLang++) compiler settings within the .xcodeproj are configured for:
C++ Language Dialect C++11 [-std=c++11]
C++ Standard Library libc++ (LLVM C++ Standard library with C++11 support)
C Language Dialect c11
All works as expected.
End Edit

linking to boost regex in gcc

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.