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

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)

Related

Error running cross-compiled code with pthread

I'm uing ARM_EABI cross-compiler to compile a code that makes use of pthreads to run at an ARM Cortex A9 simulation.
While I'm able to compile it with no problems (just as I've did with others non-pthread applications, which ran fine in the simulation), I'm having an error message when trying to run my pthread application at the simulated ARM (which is running Linux as OS). It's the following:
./pttest.exe: /lib/libpthread.so.0: no version information available (required by ./pttest.exe)
I did my research and found out that's because it's a dynamic lib, and I'm compiling the application with a higher version than the one available on my simulator.
My question is: how to find force my cross-compiler to compile the application with the same pthread lib version of my simulator? Is there anywhere I can download different versions of pthreads? And how to set it?
Sorry, I'm quite a newbie in that area.
Try compiling your application statically, e.g.
gcc -static -o myapplication myapplication.c

why mingw32 and tdm-gcc64 behave differently using external gcc

I am trying to cabal install a component of wxHaskell (Haskell platform 2013.2 against wxWidgets 3.0).
I was able to compile the git version with 32 bit mingw from mingw.org. But in the end, the installed wx cannot function correct, and running a minimal example gives runtime exceptions in wxc.dll. So I try to compile the same thing under TDM-GCC 4.8.1 64bit, since the wxWidgets people provide their binary in the form of TDM-GCC compiled binaries.
But I immediately run into compilation errors with TDM-GCC, telling me
error: 'strnlen' was not declared in this scope
What surprises me is that even though both mingw32 and TDM-gcc uses the same external gcc from Haskell Platform c:\HaskellPlatform\2013.2.0.0\mingw\bin\gcc.exe, one would give an error while the other compiles fine.
The first file causing problem is src\cpp\eljaccelerator.cpp. It compiles OK under mingw32:
...
c:\HaskellPlatform\2013.2.0.0\mingw\bin\gcc.exe -Wl,--hash-size=31 -Wl,--reduce-
memory-overheads -Isrc/include -IC:/MinGW/msys/1.0/local/include/wx-3.0 -IC:/Min
GW/msys/1.0/local/lib/wx/include/msw-unicode-3.0 -D__WXMSW__ -DWXUSINGDLL -D_LAR
GEFILE_SOURCE=unknown -DwxcREFUSE_MEDIACTRL -DBUILD_DLL -c src\cpp\eljaccelerato
r.cpp -o dist\build\src/cpp/eljaccelerator.o
but gives an error under TDM-gcc:
Building wxc
c:\HaskellPlatform\2013.2.0.0\mingw\bin\gcc.exe -Wl,--hash-size=31 -Wl,--reduce-
memory-overheads -Isrc/include -IC:/mingw/msys/1.0/local/include/wx-3.0 -IC:/min
gw/msys/1.0/local/lib/wx/include/msw-unicode-3.0 -D__WXMSW__ -DWXUSINGDLL -D_FIL
E_OFFSET_BITS=64 -DwxcREFUSE_MEDIACTRL -DBUILD_DLL -c src\cpp\eljaccelerator.cpp
-o dist\build\src/cpp/eljaccelerator.o
In file included from C:/mingw/msys/1.0/local/include/wx-3.0/wx/crt.h:19:0,
from C:/mingw/msys/1.0/local/include/wx-3.0/wx/string.h:4305,
from C:/mingw/msys/1.0/local/include/wx-3.0/wx/memory.h:15,
from C:/mingw/msys/1.0/local/include/wx-3.0/wx/object.h:19,
from C:/mingw/msys/1.0/local/include/wx-3.0/wx/wx.h:15,
from src/include/wrapper.h:20,
from src\cpp\eljaccelerator.cpp:1:
C:/mingw/msys/1.0/local/include/wx-3.0/wx/wxcrt.h: In function 'size_t wxStrnlen
(const char*, size_t)':
C:/mingw/msys/1.0/local/include/wx-3.0/wx/wxcrt.h:173:92: error: 'strnlen' was n
ot declared in this scope
C:/mingw/msys/1.0/local/include/wx-3.0/wx/wxcrt.h: In function 'size_t wxStrnlen
(const wchar_t*, size_t)':
C:/mingw/msys/1.0/local/include/wx-3.0/wx/wxcrt.h:187:95: error: 'wcsnlen' was n
ot declared in this scope
Failed to install wxc-0.90.1.1
I was wondering if anyone has any similar experience. Any idea what went wrong and how to fix compilation for TDM-GCC? I tried adding #include <cstring> to wxcrt.h but it doesn't change anything.
FYI, I have compiled wxWidgets 3.0.0 from source in mingw and tdm-gcc versions respectively, using
./configure --enable-stl && make && make install
I can provide more details if needed.
First of all, wxWidgets certainly does work with MinGW, the fact that only TDM binaries are provided simply means that someone volunteered to provide the latter but not the former. But all three popular versions of MinGW (the two already mentioned and MinGW-w64) do work, so there must be something wrong with the build...
However while they all work, they are certainly different compilers, so what do you mean that they both use the same gcc binary? It must be either a MinGW one or a TDM one, but it can't be both at once.
It's also very suspicious that the configure detects different flags to use for the large file support. Look at config.log, something must have gone wrong and there must be some errors in the initial stage in it.

Statically Linking NCurses Gives Error, for use in BusyBox environment

I wrote a very simple ncurses program to be run in BusyBox environment. However, it seems like that I cannot get my program to compile with everything. I used:
g++ menu.cpp -ohello -lncurses --> Works fine
g++ -static menu.cpp -ohello -lncurses --> Undefined reference to SP (many times)
I found this question but it ignores linking to ncurses. I need a very single executable. My targeted environment is fixed, so I do not concern portability.
You should paste the exact compiler calls and the exact error messages that you are getting.
Do you have a static version of the ncurses library?
More importantly, do you have a static version of the ncurses library compiled for your target environment? For example your target environment may be using ulibc instead of glibc or it could even be a whole different platform (hint: tell us what your target platform is).
Are you certain that you are compiling with the right flags? The compiler flags that you are showing seem more suited to compiling an application for use in the build host environment...

"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.

Setting up G++ or ICC for mpi.h on Ubuntu

I have never done any major programing outside of VS08.
I am trying to compile a program called LAMMPS with either of the two relevant make files. One calls g++ and the other calls icc (Intel's compiler).
icc produces this error:
icc -O -DLAMMPS_GZIP -DMPICH_SKIP_MPICXX -DFFT_FFTW -M write_restart.cpp > write_restart.d
write_restart.cpp(15): catastrophic error: cannot open source file "mpi.h"
#include "mpi.h"
and g++ throws this error
g++ -g -O -DLAMMPS_GZIP -DMPICH_SKIP_MPICXX -DFFT_FFTW -M verlet.cpp > verlet.d
pointers.h:25: fatal error: mpi.h: No such file or directory
compilation terminated.
The mpi.h file is located in /usr/lib/openmpi/include
It is my understanding that I need to set that $PATH variable which reads
bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/X11R6/bin:/opt/intel/bin:/usr/lib/openmpi/include:
and $LD_LIBRARY_PATH which currently reads
/usr/lib/openmpi/lib:
SO, how does one include the mpi.h file? So that either icc or g++ find it?
mpi.h is a header for MPI library. That would be included if you use mpic++ MPI compiler wrapper instead of g++ in your makefile. mpic++ will call the appropriate compiler. From what you describe you have openmpi package installed on your ubuntu machine.
For more info, you need to consult the manual, e.g.
http://lammps.sandia.gov/doc/Section_start.html#2_2 (for LAMMPS)
and perhaps you need to see openmpi manual as to how to set up additional compiler. Not sure if this can be done after openmpi itself has been built. By default I think in Ubuntu openmpi compiler wrappers would only call g++. CMIIW.
Okay, so I got it to work with g++ when setting up cc as "mpic++.mpich2" instead of "mpic++"
you can try compile using openmpi make file in /src/MAKE
make openmpi
in my case, this option was successful

Resources