Compiling multithread code with g++ (-Wl,--no-as-needed NOT working) - c++

My problem is actually described here: Compiling multithread code with g++.
But the answer regarding the work around by using "-Wl,--no-as-needed" is not working for me.
I've added -Wl,--no-as-needed -pthread -std=c++0x in different orders also, but I still get the:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted"
What to do?
Info:
Ubuntu 12.04LTS
Running Eclipse CDT
g++ v4.8.1
Edit:
I tried building with -Wl,--no-as-needed -lpthread -std=c++0x with no luck.
The code:
#include <iostream>
#include <chrono>
#include <thread>
void foo()
{
std::cout << "Thread 1 created.." << std::endl;
}
int main()
{
std::thread t1(foo);
t1.join();
return 0;
}
Edit:
So unfortunately none of your suggestions worked. I decided to use Boost instead.

it's -Wl,--no-as-needed not -Wl,--no_as_needed, you use the hyphen
-pthread is a flag for the compiler, not the linker, the right one for the linker is -lpthread
Mingw doesn't always comes with the same threading library, there are more than 1 options for multithreading with MinGW, you should document yourself about this according to your MinGW build

g++ filename.c -std=c++11 -lpthread
i am compiling your code with above command its working perfect.

Related

std::thread is not being found on gcc-8.2.2 with flag option -m32. I'm using mingw

I was trying to compile a cpp program for a school project. It uses libbgi for graphics, which works for 32-bit target. For this project i wanted to add some multithreading with std::thread, but the compiler say i couldn't find std::thread.
I tried a compiler with a lower version, with c++11 support.
#include <iostream>
#include <thread>
int main(){
std::thread t([](){
std::cout << "Hello\n";
}
);
t.join();
}
this program compiles fine with -m64, but not with -m32.
I believe you might need to link with pthread and use c++0x.
g++ -std=c++0x -m32 main.cpp -pthread -o main
I recreated your error and it was solved using these flags.

gmp.h missing when trying to use boost library

I am trying to use the boost library with QT on windows. I've successfully build the library and also managed to include it in my project. However, on including gmp (#include "boost/multiprecision/gmp.hpp") and creating an object (boost::multiprecision::mpz_int myint;) I get the following error:
C:\Users\Laurenz\Documents\libraries\boost_1_66_0\include\boost\multiprecision\gmp.hpp:31: error: gmp.h: No such file or directory
And indeed, I haven't been able to find any such file in the boost directory. What did I do wrong?
Install the dependency and link to it. (See What is an undefined reference/unresolved external symbol error and how do I fix it?)
Alternatively, consider not using GMP, using cpp_int.hpp instead.
Since you already installed the GMP library, here's the last step:
Live On Coliru
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
int main() {
boost::multiprecision::mpz_int i("1238192389824723487823749827349879872342834792374897923479");
std::cout << pow(i, 3) << "\n";
}
Note the -lgmp flag at the end of the compile/link command:
g++ -std=c++11 -O2 -Wall -Wextra -pedantic main.cpp -o demo -lgmp
Running it:
./demo
1898298004808110659499396020993351679788129852647955073547637871096272981567489303363372689896302906549189545322451852317205769760555889831589125591739044248515246136031239

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

C++ CodeLite Enable Multithreading?

I'm experiencing a bit of strange behaviour when I try to compile any program that uses multithreading in CodeLite.
I get the error:
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not premitted.
after some quick googling I found out I had to add "-pthread" to the compiler options.
Note: that CodeLite puts -l in front of libraries, so it does use -lpthread
After I clean, and rebuild the project, I still get the error though. As far as I can tell the build log looks fine?
The truly frustrating part comes about when I compile it manually via the command line, It works just fine.
I've searched, but none of the solutions seem to work for me? perhaps I've missed a step somewhere?
here's my test code.
I should also note I'm using Ubuntu14.04, and CodeLite 9.1.0
#include <iostream>
#include <string>
#include <thread>
void test()
{
std::cout << " Look it works! \n";
}
void int main(int argc, char** argv)
{
std::thread thrd_1 = std::thread(test);
thrd_1.join();
return 0;
}
Any help would be greatly appreciated!
You are passing -pthread in the compiler options. You need to pass it
in the linker options, and in the linker options you do not need
to specify pthread as a library. The -pthread option means
do whatever it is that links the posix threads library on this platform.
$ g++ -c -O0 -std=c++11 -o main.o main.cpp
$ g++ -o threadtest -pthread main.o
$ ./threadtest
Look it works!

C++11 Thread not working [duplicate]

This question already has answers here:
Why does this simple std::thread example not work?
(5 answers)
Closed 7 years ago.
My programs looks like below
#include <iostream>
#include <thread>
#include <exception>
void hello()
{
std::cout << "Hello world!!!!" << std::endl;
}
int main()
{
std::cout << "In Main\n";
std::thread t(hello);
t.join();
return 0;
}
When I compile it using the following command I get no errors
g++-4.7 -std=c++11 main.cpp
But when I run it I get the following error
In Main
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Could someone help me with where I am going wrong?
When I use C++11 threads with GCC, i use:
g++ -std=c++0x -pthread -g main.cpp
That works for me.
When compiling your code with g++, use the -pthread option.
Below is the answer I find from stackoverflow:
In g++ is C++ 11 thread model using pthreads in the background?
Everybody already answered that you need the -pthread argument passed to the compiler. Almost for sure it won't change in 4.8 but according to http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52681 the exception will at least have a nice message stating what's wrong.
You might need to link against the pthread library
You can try with,
g++ --std=c++11 -lpthread main.cpp