Error compiling thread program in codeblocks 12.11 - c++

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.

Related

Using C++ Multithreading library <thread> is giving error 'thread' is not a member of 'std'

I know it is a repeat question but non of the answers that I read is addressing the issue. Hence I am Posting the question again.
I wrote this very simple program
#include<iostream>
#include<thread>
using namespace std;
void func1(char ch){
for(int i=0; i<100; i++){
cout<<ch;
}
}
int main(){
std::thread worker1(func1, 'a');
std::thread worker2(func1, 'b');
getchar();
return 0;
}
The above code when compiler with (MinGW.org GCC-6.3.0-1) 6.3.0(C++ 14) gives me error as thread' is not a member of 'std'. However, I am able to go to the definition of thread at location C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++\thread. The same code when compiled on Ubuntu with compiler g++ (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 works perfectly fine. I don't seem to find a reason why it should not work on Windows.

Eclipse c++ giving me Invalid arguments if I write thread using lambda

When trying to make thread using lambda with following code:
#include <iostream>
#include <thread>
using namespace std;
int main() {
thread t([] {
cout << "hello\n";
});
t.join();
return 0;
Eclipse says:
Description Resource Path Location Type
Invalid arguments '
Candidates are:
thread(const std::thread &)
thread(std::thread &&)
' cpptest.cpp /cpptest/src line 14 Semantic Error
Code compiles successfully, but that error is annoying, I also tried C/C++ Build > Settings > GCC C++ Compiler > Dialect > Language Standard = ISO C++17, but without luck.
There may be problem with cpp checking setting, which are not set to higher cpp version, but I can't see that option in project setting.

Eclipse Neon 3 - C++ 11 Threads

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 :)

Error using alias declaration

I'm trying to compile an easy program that use the alias declaration.
This is the code:
#include <iostream>
using namespace std;
using in = int;
in main ()
{
in a = 1;
cout << a << '\n';
return 0;
}
The command I use to compile is g++ -std=c++0x program_name.cxx, using the built-in terminal in Kate on Ubuntu OS.
But it doesn't work! Any suggestion?
(instead using typedef int in; it works).
Compile in C++11 mode. Type aliasing is supported only in C++11. I suspect the g++ version that use is older and doesn't fully support c++11, hence fails with c++0x.
Compile with: g++ -std=c++11 file.cpp
and it works.
By the way, it seems to be a terrible idea to alias int in such a way.

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