Compiling Multithread Code With G++

Compiling multithread code with g++

The answer was provided by a kind member of SO C++ chat.

It looks like this behaviour is caused by a bug in gcc.

The workaround provided in the last comment of that bug discussion does work and solves the issue:

-Wl,--no-as-needed

Compiling multithread code with Code::Blocks GNU compiler

Your first problem is that -lpthread is a linker option, so it belongs on the linker line (the second command) rather than the compilation line. (Note that the order of the parameters can matter; I got it to work by putting -lpthread on last. I also tried using -pthread instead of -lpthread, which did appear to work and was also less sensitive to where it was put on the linker line. But again, it's a linker option, not a compile option.)

After fixing that, I was able to get your program to compile and run, but it exited with a different exception: terminate called without an active exception. To fix this problem, call thread_fct.join(); in main(). (All threads must be joined, detached, or moved-from before they go out of scope, or your program will abort.)

Multithreaded code won't compile using g++, but is fine with clang++

It seems, that std::future & std::async are not implemented on the armel architecture for some reason.

I can't really find out why is this (some argue on mailing lists, that they are not implemented by design, some others say this is a bug) and what is the current state of the problem.

However, I've also found a reply that stated this may be already resolved in the newer versions of libstdc++(My system is running the Testing version of debian, I do not have these versions yet, and I don't plan to get the package from unstable repos, so I'll just wait for it).

problems using threads in C++ on windows 10 (using g++ as compiler)

For anyone else dealing with this problem: The simplest solution is to download mingw-64 and use their compiler instead.

Then use the -std=gnu++11 or -std=c++11 arguments to enable support for the ISO C++ 2011 standard and by extension support for threads (do note: This support is currently experimental, though that has not given me any problems so far).

Compiling with g++ using multiple cores

You can do this with make - with gnu make it is the -j flag (this will also help on a uniprocessor machine).

For example if you want 4 parallel jobs from make:

make -j 4

You can also run gcc in a pipe with

gcc -pipe

This will pipeline the compile stages, which will also help keep the cores busy.

If you have additional machines available too, you might check out distcc, which will farm compiles out to those as well.

Why doesn't my compiler recognize #include thread (c++)?

MinGW-w64 by default comes with native (Win32) instead of POSIX threads support, and unfortunately there is currently no Win32 gthreads implementation (the threading subsystem of libstdc++), hence no threading functionality in GCC.

You need to switch from x86_64-w64-mingw32-g++-win32 to x86_64-w64-mingw32-g++-posix package to get std::thread working in MinGW-w64.

The question mingw-w64 threads: posix vs win32 discusses this issue in more detail.

threading program runs with g++ but not with gcc

If you’ve written C++ code with GCC, you’ll know that you need to use the program g++, both for compilation and linking. For multi-module programs, this means every .cpp file gets compiled with g++, and then the entire program must be linked separately using g++. If you try to link the program using gcc, it will almost work, but you’ll get a lot of “undefined reference” errors, like this:

test.cpp:(.text+0x11): undefined reference to `std::cout'
The need to use g++ to link the entire program causes trouble when you have a very complicated build process you don’t have full control of. For instance, I’m trying to link C++ code with Mercury, and I have to use the Mercury linker, which in turn calls gcc.

So just a quick tip: If you are forced to use gcc to link the program, just add the library “stdc++”, as you would any other library, and it will work. That is, add the option “-lstdc++” to your GCC linker command line. For example:

g++ -c hello.cpp

gcc -lstdc++ -o hello hello.o



Related Topics



Leave a reply



Submit