boost::regex segfaults when using capture - c++

I get a seg fault for the simple program below. It seems to be related to the destructor match_results.
#include <iostream>
#include <vector>
#include <string>
#include <boost/regex.hpp>
using namespace std;
int main(int argc, char *argv)
{
boost::regex re;
boost::cmatch matches;
boost::regex_match("abc", matches, re.assign("(a)bc"));
return 0;
}
edit: I am using boost 1.39

boost::regex is one of the few components of boost that doesn't exist solely in header files...there is a library module.
It is likely that the library you are using was built with different settings than your application.
Edit: Found an example scenario with this known boost bug, where boost must be built with the same -malign-double flag as your application.
This is one of several possible scenarios where your boost library will not have binary compatibility with your application.

Which version of boost are you using?
I compiled the above example with boost 1.36 and I don't get any seg faults.
If you have multiple boost libraries make sure that at runtime you're picking up the correct version.
Boost regex requires to be compiled against library -lboost_regex-gcc_whatever-is-your- version
In my case:
g++ -c -Wall -I /include/boost-1_36_0 -o main.o main.cpp
g++ -Wall -I /include/boost-1_36_0 -L/lib/boost-1_36_0 -lboost_regex-gcc33-mt main.o -o x
to execute:
LD_LIBRARY_PATH=/lib/boost-1_36_0 ./x
You would point to the location of boost include/libs on your system, note the version of gcc and m(ulti) t(hreaded) in library name - it depends on what you have compiled, just look in your boost lib directory and pick one version of regex library from there.

You are using temporary variable from which you want to obtain matches. I think, that your problem will resolved, if instead "abc" you will use following:
string a("abc);
regex_match(a, matches, re.assign("(a)bc"));

I was having the same problem. I tried the solution posted by Drew Dormann, but it didn't work. Then I discovered that I was actually linking against 1.40, but for some reason the headers were for 1.37. Once I downloaded the correct headers (1.40), it stopped segfaulting.
I noticed it when I had compiled with the debugging symbols -g and run a dbg backtrace..
Hope that helps...

Related

Trying to use Boost library on Codeblocks gives an undefined reference

I'm trying to use the boost library on CodeBlocks, but I'm new to it and I can't seem to be able to link it properly.
The boost folder(version 1.70) is in the same folder of my main.cpp, and the library I'm trying to access is libboost_filesystem-mgw92-mt-x64-1_70.a;
Here is my code:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::path l_path("C:\\Hello.txt");
if(boost::filesystem::exists(l_path))
{
std::cout<<"exists!"<<std::endl;
}
else
{
std::cout<<"no";
}
return 0;
}
And some screenshots of my settings and of the error
Thank you!
Undefined reference to _Unwind_Resume suggests that you build Boost with different compiler than your project or you choose different type of exception handling.
Check if you're using the same compiler in both cases.
It might be also caused by building your project using gcc instead of g++. You should check that as well. In this case switch to g++ or explicitly link against libstdc++, by adding -lstdc++ to compiler flags.

Compiling boost::asio example on Windows

I am trying to switch to Windows environment from Linux, but find it a very hard path.
This time I wanted to test if I can work with boost library.
I had problems with compiling boost on windows, so I downloaded precompiled version. I unpacked everything and tested positively that I can compile the header-only librariers.
Then I copied some simple boost::asio example. I set up everything in Eclipse. Compilation went fine, but during linking I got 'undefined reference' problem to 'boost::system' internal stuff.
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/Users/jacek/cpp/boost_1_62_0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
So I added '-lboost_system', as well as the path to the libraries directory, to my linking options. But this did not help.
g++ "-LC:\\Users\\jacek\\cpp\\boost_1_62_0\\lib64-msvc-14.0" -o TestAsio.exe "src\\Main.o" -lboost_system
I checked the libraries directory and found there is a bunch of files containing 'boost_system' in the name. They are:
libboost_system-vc140-mt-1_62.lib
libboost_system-vc140-mt-gd-1_62.lib
libboost_system-vc140-mt-s-1_62.lib
libboost_system-vc140-mt-sgd-1_62.lib
libboost_system-vc140-s-1_62.lib
libboost_system-vc140-sgd-1_62.lib
I did not know which I should use. I tried adding 'libboost_system-vc140-mt-1_62' to the linking options, I tried all other files, I tried renaming the files to the linux pattern 'libboost_system.a', but nothing worked.
g++ "-LC:\\Users\\jacek\\cpp\\boost_1_62_0\\lib64-msvc-14.0" -o TestAsio.exe "src\\Main.o" -llibboost_system-vc140-mt-1_62 -llibboost_system-vc140-mt-gd-1_62 -llibboost_system-vc140-mt-s-1_62 -llibboost_system-vc140-mt-sgd-1_62 -llibboost_system-vc140-s-1_62 -llibboost_system-vc140-sgd-1_62
What am I doing wrong here?
Please help...
YotKay
I solved it myself with the help of a comment from this post: boost asio example compilation error
It looks like the precompiled version of Boost is created with Visual Studion and is NOT COMPATIBLE with G++. I if I decided to install MinGW then I cannot use the precompiled version of boost, but must compile it myself using g++.
I did that.
Now I have libraries compiled with G++.
I specify the path to the boost system library like that:
c:\Users\jacek\cpp\boost_1_62_0\libraries\boost\bin.v2\libs\system\build\gcc-mingw-6.2.0\debug\link-static\
and add this option:
-lboost_system-mgw62-d-1_62
Now the problem with boost::system disappears. However, another one pops up with boost asio, but luckily the answer is here: MinGW linker error: winsock
The example works fine now on my Windows 10 laptop.
#include <boost/asio/io_service.hpp>
#include <boost/asio/steady_timer.hpp>
#include <chrono>
#include <iostream>
using namespace boost::asio;
int main()
{
io_service ioservice;
steady_timer timer{ioservice, std::chrono::seconds{3}};
timer.async_wait([](const boost::system::error_code &ec)
{ std::cout << "3 sec\n"; });
ioservice.run();
}

c++: How to remove libstdc++.so.6 dependencies

I have 2 program I wrote on my windows computer using Visual Studio 2013. They run fine and work perfectly on my computer, but when I brought them over to my school account that is on a Linux machine, a problem arose. They compile and 1 ran, but the other did not. The one that did not run gave me an error:
.../lib/compat/libstdc++.so.6: version CXXABI_1.3.2 required by...
I have been doing research and I can't seem to find out what in my program would be using libstdc++.so.6, I'm not even really sure what it is or does. Since I am on a student account I can't go installing it using sudo, and it is a homework so I can't submit it using my own libraries.
Any Idea on what my program might be using that would require libstdc++.so.6?
I have 3 files: main.cpp, LinkedList.cpp and LinkedList.h.
I think it might be in main.cpp because I think it stems from a library I am including and main.cpp is the only one that uses outside libraries. Here is the list of libraries it uses:
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <ctype.h>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <bitset>
#include <algorithm>
#include "LinkedList.h"
Thanks in advance!
You are trying to run a program linked against one version of the libraries under another set. That should work fine as long as the library versions aren't too far apart. In your case, the difference between libraries is just too large.
GCC (C++ in particular) has changed quite a bit lately, some programs that used to compile and run fine now blow up or don't compile at all (due to language changes, compiler bugs accepting broken code, ...), and the library ABI has also changed. Your best bet is to carry source code around, and make sure you got compatible language versions on both ends. If that is inconvenient, a solution is to make sure you have the same compiler (and other environment) at both places. The easiest way to get this is to install the same distribution and version.
First you can't remove the dependencies of libstdc++.so.6, because it's a standard C++ library.
To solve your problem you have to check whether your libstdc++.so have the right version
strings /usr/lib64/libstdc++.so.6|grep GXXABI_1.3.1
if there have no matching version, you will have 2 methods like these:
update your gcc on your school's linux OS
yum intsall gcc
download a matching libstdc++.so from this website:
download gcc || download matching libstdc++
then replace the libstdc++.so to /usr/lib64/libstdc++.so.6.*
SOLUTION
I went through a few steps to find my solution. Originally I could compile my program but could not run it.
1) My first step to solve the issue was to change my method of compiling. Originally I compiled my program with the following: g++ main.cpp LinkedList.cpp -o output. I changed it to: g++ -static main.cpp LinkedList.cpp -o output which allowed me to compile and run. This worked but static is a method to dynamically link libraries. This prevents linking with the shared libraries. This is not a good solution because it takes a lot longer and increases the file size of the executable, so I wanted to improve.
2) The second thing I did was remove using namespace std. Yes, I cheated and used it. So I went through my program and added std:: to the appropriate places.
3) The last thing I did was clean up my code. I was using a lot of libraries because my program was a large and complicated program. I was using all of the libraries I had listed in my original post. I went through my code and anywhere I was using a function from a library I would try and write my own code that would do the same thing which would result in my program not depending on those libraries. I was able to replicate a decent amount of these dependent foreign functions with my own which added lot of code, but it allowing me to remove some of these includes. My list of includes is now:
#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "LinkedList.h"
#include <math.h>
I am not sure exactly which step resolved my issue, but now I can compile with my preferred method, g++ main.cpp LinkedList.cpp -o output, and my program runs fine.
I hope this helps someone.

Compiling and Linking Leda 6.3

I am new to LEDA and I am working on LEDA6.3 Free Edition on OpenSuse 12.1.
As a start I tried to write a simple code "Hello LEDA world" as in the manual http://www.leda-tutorial.org/en/official/ch01s02.html.
#include <LEDA/string.h>
#include <iostream>
using leda::string;
using std::cout;
int main()
{
string msg = "Hello LEDA world!";
cout << msg << "\n";
}
The compilation phase works fine and I generated the .o file.
But they mentioned that this file needs to be linked to the LEDA library and the only library available in the Free edition is the libleda.a and libleda.so
I am trying to link using the following command:
g++ -o welcome welcome.o -L$LEDAROOT -llibleda
What I get is:
/usr/lib64/gcc/x86_64-suse-linux/4.6/../../../../x86_64-suse-linux/bin/ld: cannot find -llibleda
I was wondering if someone could help me with how to link my file with the leda library ? so I can get it to run.
Thanks all.
It is unlikely that the library file is called liblibleda.a or liblibleda.so: when the linker sees a an option of the form -l<name> it will search for a library file named lib<name>.so (for shared libraries) or lib<name>.a (for static libraries). You probably just want to use the option -lleda (unless the LEDA developers did something clever and called the library something like libeda.a so you'd use -leda).
Whether this works will also depend on the compiler options being consistent between the installation and your build.

Cannot link to MySQL libraries for C++

I've been trying to use MySQL and C++ together but can't seem to get started as I cannot seem to gain usage of the relevant libraries.
I am on Win7 using MinGW compiler and working in Netbeans.
I have the code:
#include <cstdlib>
#include <iostream>
#include <my_global.h>
#include <mysql.h>
using namespace std;
int main(int argc, char **argv)
{
cout << "MySQL client version: " << mysql_get_client_info();
}
But netbeans cannot find my_global.h or mysql.h.
In properties of the project I've linked to the library libmysql.dll.
Also present in the same directory is mysqlclient.lib but I can't find a way to link to that as the NetBeans linker doesn't seem to register that extension type.
Any help would be greatly appreciated.
C
---PROGRESS
I went into NetBeans' Properties->Build->C++ section and added the include directory of my MySQL installation in the 'Include Directories' section. This has solved the above issue of not finding my_global.h or mysql.h but now it cannot find crtdbg.h...
Actually had crtdbg.h in an old Visual Studio installation, moved it and all the other .h files there over to my MinGW includes folder. Seems to find the .hs now but fails with loads of errors, probably an issue with the Visual Studio .h files not being compatible with MinGW. Back to the drawing board.
Set the include directories, mate. It is under Tools->Options->C++->Code Assistance. Add the path where the my_global.h is.
See this forum post.
I'm not sure in Windows, but in Linux you can use the mysql_config tool to get the correct flags to compile a client application:
For compiling:
$ mysql_config --cflags
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
And for linking:
$ mysql_config --libs
-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient
Now, if you are using MinGW, options should be similar, probably dropping the *_LINUX ones.
My bet is that you are simply missing the -I<path_to_include_dir> bit.