Eclipse Neon 3 - C++ 11 Threads - c++

I have a problem with using Threads in C++. I read this topic:
How to make CDT/Eclipse work with C++11 threads?
But it didn't help at all :(
So, from the beginning. I'm trying to 'convert' big project from Visual Studio into Eclipse and I'm forcing with a problem with threads. So I created another Eclipse project in order to test that.
#include <iostream>
#include <thread>
void worker()
{
std::cout << "hello from worker" << std::endl;
}
int main(int argc, char **argv)
{
std::thread t(worker);
t.join();
}
But, I get:
error: 'thread' is not a member of 'std'
In GCC C++ Compiler->Miscellaneous I have
-c -fmessage-length=0 -std=c++11 -pthread
In MinGW C++ Linker->Libraries I have
pthread
Even in MinGW C++ Linker->Miscellaneous I have
-std=c++11
I'm a fresh user of Eclipse and I have no idea what I'm doing wrong.
I would be very grateful for any tips :)

Related

G++ compiled application crashes when using std::string or char pointer

I just installed MinGW because I wanted to write code using a lightweight code editor instead of Visual Studio or Codeblocks, both of which I never had a problem with. So the code compiles and works, but for some reason it crashes every time I try to use a string or char pointer. For example this code:
#include <iostream>
int main()
{
std::string a;
a = "Hello";
std::cout << a;
std::cin.get();
}
throws this error when executed:
but this code:
#include <iostream>
int main()
{
std::cout << "hello";
std::cin.get();
}
Works perfectly:
(I have tried both with std::string and const char* with no difference)
I have Visual studio 2017 installed, I don't know if that could be the issue.
MinGW version 6.0.3.
PD: I only added C:/MinGW/bin directory to Path environment, don't know if I should add any other directory
Fixed it! For some reason g++ by default links the std library dynamically. As my program didn't find the dll it was not able to link std::string and std::cout. The solution was to pass the -static-libstdc++ argument to g++.
TL;DR:
Instead of compiling directly like this:
g++ somesourcefile.cpp
try this argument:
g++ somesourcefile.cpp -static-libstdc++

Thread doesn't work with an error: Enable multithreading to use std::thread: Operation not permitted

I created and executed a simple thread on my system. when I execute this program, I have the error message : Enable multithreading to use std::thread: Operation not permitted
some details about my system :
linux ubuntu 13.10
g++ 4.8.1
I compile the source code including the library pthread
The source code:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
It seems that you are trying to use C++11 threads. If it is true, then
correct #include <thread> and #include <iostream>, i.e. do not use " in these lines and add # in front of them.
compile with g++ -std=c++11 q.cpp -lpthread (dependency order matters for newer g++)
Hint: when you are using threads in a static linked library and use this library in an executable, then you have to add the flag -pthread to the link command for the executable. Example:
g++ Obj1.o Obj2.o MyStaticLib.a -o MyExecutable -pthread

Error compiling thread program in codeblocks 12.11

my thread program is :
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
I have the same problem. Unfortunately the version of GCC Code::Blocks uses does not support the features of C++11 that you want. Turning on the option -std=c++0x will only ugrade the compiler to a less recent version of the new Standard. That means you will only enable basic support of C+11.

Error compiling thread program in codeblocks

My thread program is:
#include<iostream>
#include<thread>
using namespace std;
void t()
{
cout<<"from thread\n";
}
int main()
{
thread i(&t);
cout <<"from main\n";
i.join();
}
but it shows following error in codeblocks:
1)'thread ' was not declared in this scope
2)expected ';' before 'i'
3)'i' was not declared in this scope
How can I solve it?I am using windows and codeblocks 12.11
First, are you on windows or linux?
If you are on linux, you must compile with C++11 support. Just pass -std=c++11 to g++.
I can't help you with windows.
Your IDE may not support C++11 yet. since thread is included in the standard since C++11. See this thread for CodeBlocks? http://forums.codeblocks.org/index.php?topic=15536.0

Simple netbeans C++ project doesn't compile

I installed Netbeans and as C++ compiler I installed cygwin. I made a simple project to test out my installation, this is the code:
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << "test";
return 0;
}
This is the error message that it gives: http://pastebin.com/jRRh7MPi
I hope you guys can help me out.
You need to either explicitly link to C++ standard library, or compile using g++ instead of gcc.