Linux - setting Code::Blocks to compile multithreading C++ code - linux

I have searched through forums here, but nothing helped to the end in my case.
I'm trying to write and compile some C++ multithreading code in Code::Blocks 13.12, for now the only code I have is:
#include <iostream>
#include <thread>
void cpp11()
{
std::cout<<"C++11 MULTITHREADING\n";
}
int main()
{
std::thread t(cpp11);
t.join();
}
Initially I could not compile and run that code (both in Code::Blocks and Terminal) because I was getting an error:
Terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not
permitted.
I found that I'm able to compile and run that code calling g++ in the Terminal like that:
g++ -std=c++11 -pthread main.cpp -o Program
but I cannot set the same in Code::Blocks.
I went to Project -> Build options, in the Compiler settings tab I selected Have g++ follow the C++11 ISO C++ language standard [-std=c++11].
In the Compiler settings -> Other options I typed -pthread.
In Linker settings -> Other linker options I typed -pthread.
It does not work, after I build and run my project in Code::Blocks I get the same error message.
Thank you for any help!

Follow instructions for this question
How do I link to a library with Code::Blocks?
in the last step, when Add library dialog pops up type pthread.
UPDATE
Again go to build options > Compiler settings . click on other options and type -pthread

Related

Build process crashes with boost and C++11

What I have:
I am writing Qt application for Linux (I work in Linx Mint 17.3 64-bit)
I use C++11 features in my Qt project (Qt ver 5.5)
I want to add libslave to my Qt project.
libslave uses deprecated (for C++11) boost::function, boost::shared_ptr, boost::bind and boost::any.
My trouble:
When I compile with gcc (v the whole project or only library with -std=c++11 flag boost crashes with many errors. Qt Creator shows about 4000 errors, but they are pretty similar and look like:
typedef boost::function< void( RecordSet& )> callback;
is not complete type
BOOST_NOEXCEPT'does not name a type
~any() BOOST_NOEXCEPT
etc...
I have tried to rewrite library with C++11 std library, but std does not containg boost::any analog, so that was bad idea.
Question:
How to compile boost (or at least libslave) with c++11?
Boost Version: 1.54 (from repo)
g++ version: 4.8.4 (from repo)
Qt version: 5.5 (downloaded from Official Site)
Linux Mint: 17.3 Rosa
UPDATE:
Example:
You can download code what I try to compile by this link.
Instruction:
Download tarball
Extract
Go to folder and just type make (all works fine)
Open MakeFile and replace CXX variable to
CXX = g++ -std=c++11
Try to make again and you'll get errors.
P.S.
To compile library you'll need libmysqld-dev, libboost-all-dev, libmysqlclient-dev.
Probably you'll need something else, but I don't remeber. Sorry.
I found the hack and it works for me.
I replace boost::bind usage in file nanomysql.h to std::bind by such strings:
...
typedef std::map<std::string, field> value_t;
typedef std::vector< value_t > result_t;
void store(result_t& out)
{
//You need specify template because of push_back has overloads
auto hack = std::bind<void(result_t::*)(const value_t&)>(&result_t::push_back, &out, _1);
use(hack);
}
...
And replace all boost::shared_ptr, boost::function to std::shared_ptr and std::function in all files in library.
After this everything compiles and work fine with -std=c++11 flag.
Whole code of nanomysql.h you can see here:
Link to code
Use actual fork of libslave - https://github.com/vozbu/libslave with support c++11. Support for mysql 5.6 and 5.7 will be soon

C++11 on Code::Blocks in Ubuntu 14.04

So I'm trying to run a simple hello world program with a thread. It's the standard hello world program that is used to introduce you to threads. I'm using Code::Blocks IDE on Ubuntu 14.04, I have gcc 4.8.2. I use the GCC compiler to build my project and I also checked the option `Have g++ follow the C++11 ISO C++ language standard [-std=c++11]' but when I build the project I get this error:
`terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted`
I've seen solution which tell me to add -pthread and -lthread somewhere but I don't know where to write this... any help?
Add "-pthread" to your linker options ("Linker settings" → "Other linker options").
source (first Google result for code blocks pthread)

Can't Access sys/socket.h using Cygwin

I need to compile C/C++ pthread and socket code in windows 8 where I've installed MicGW GCC and G++ 4.7.
When I compile my test code using g++ test.cpp -o test
Code is:
#include<iostream>
#include<sys/socket.h>
#include<sys/types.h>
using namespace std;
int main() {
cout<<"Got Socket";
}
This gives error fatal error: sys/socket.h not found and the
same occur with types.h
The error I found is that cygwin is using MicGW GCC and g++ but I want it to use its own instead of MinGW's so that I can include Linux libraries.
I have not been able to reproduce your problem on my Cygwin.
Are the headers present in /usr/include/sys?
Search the sys folder in [installed directory]/usr/include if the socket.h was not there download one Copy it there manually.
don't afraid of manual works ;-)

Eclipse CWT crashes with grey window

I'm writing an OpenCV program for university and got some troubles with Eclipse.
I am running a 64bit Linux Mint (13) with Eclipse Juno. I installed OpenCV using this guide, except I installed Version 2.4.3.
Since the project my partner on MacOS shared with me didn't work, I created a plain C++ Project. I added the information needed in the build settings as followed:
Cross GCC Compiler -> -I -> "/usr/local/include/opencv" and "/usr/local/include/opencv2"
Cross G++ Linker -> Libraries -> -I -> "cv" and "highui"
-> -L -> "/usr/local/lib"
This is the code I'm using. Right, it does nothing:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main(int argc, char** argv) {
return 0;
}
So, everytime I'm running this, i get an ugly grey window, like a message window would look like, but nothing in it. This is the same es the actual program with code in it, so it's nothing to do with OpenCV commands.
Sometimes, this window disappears with killing Eclipse, with doens't response then. Sometimes, it even survives that and I have to start a new X session.
This is what it looks like:
The error messages you can see are not related to the crash itself, but shown before.
Sometimes it just says "Binary not found", which those messages come from. It somehow doesnt find the libraries.
This is pkg-config --libs opencv
/usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_contrib.so
/usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so
/usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_gpu.so
/usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgproc.so
/usr/local/lib/libopencv_legacy.so /usr/local/lib/libopencv_ml.so
/usr/local/lib/libopencv_nonfree.so /usr/local/lib/libopencv_objdetect.so
/usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_stitching.so
/usr/local/lib/libopencv_ts.so /usr/local/lib/libopencv_video.so
/usr/local/lib/libopencv_videostab.so
This is pkg-config --cflags opencv
-I/usr/local/include/opencv -I/usr/local/include
It hast to do something with my OpenCV/CWT setup I guess. Since I tried out a lot of variations, does anybody got any idea what I did wrong?

"Compiler threading support is not turned on."

Normally I can google my way around and find solutions, but not this time.
I'm using 64 bit Linux Ubuntu 11.04 to compile a 32 bit windows application. I'm using i586-mingw32msvc-gcc to compile my C++ files.
test.cpp:
#include <boost/asio.hpp>
makefile:
i586-mingw32msvc-gcc -c -m32 -mthreads -o test.o test.cpp
Error:
boost/asio/detail/socket_types.hpp:
# include <sys/ioctl.h>
doesn't exist.
Added to makefile: -DBOOST_WINDOWS
Error:
# warning Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately
Ok, added to makefile: -D_WIN32_WINNT=0x0501
Error:
# error "Compiler threading support is not turned on. Please set the correct command line options for threading: -pthread (Linux), -pthreads (Solaris) or -mthreads (Mingw32)"
Yet I did specify -mthreads.
Adding -DBOOST_HAS_THREADS might be sufficient (see # elif defined __GNUC__ from the offending header). But it's likely/possible that your boost installation has been crafted to support your build environment and not your target. Try building it yourself with your cross-compiling toolchain.
It turned out that I had a set of #undef and #defines to force the GLIBC version to something that allowed me to compile for Linux (not cross compile) RHEL5, which otherwise would give me all kinds of other errors. Turns out that when cross compiling for windows using mingw, that force feeding the GLIBC version causes boost to take a strange path leaving various aspects undefined, including the bahavior or availability of threading. I surrounded it with an #ifndef _WIN32 which made the problem go away.
Maybe that -mthreads argument needs to come last.

Resources