The problem occurs when compiling the vim plugin color_coded, and can be summarized as follows:
With g++ -std=c++14, symlink can not be found in unistd.h.
With clang++ -std=c++14, mutex is not supported on cygwin.
So, how to compile this code on cygwin?
#include <unistd.h>
#include <mutex>
#include <thread>
#include <iostream>
std::mutex io_mutex;
auto foo(){
std::lock_guard<std::mutex> guard{io_mutex};
std::cout << &symlink << "\n";
}
int main(){
std::thread t1{foo}, t2{foo};
t1.join();
t2.join();
}
The following are the errors.
with gcc 5.4.0 and g++ -std=c++14:
t.cpp: In function 'auto foo()':
t.cpp:9:18: error: 'symlink' was not declared in this scope
std::cout << &symlink << "\n";
With clang 3.7.1 and clang++ -std=c++14:
In file included from t.cpp:2:
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:699:10: error: thread-local
storage is not supported for the current target
extern __thread void* __once_callable;
^
/usr/lib/gcc/i686-pc-cygwin/5.4.0/include/c++/mutex:700:10: error: thread-local
storage is not supported for the current target
extern __thread void (*__once_call)();
^
2 errors generated.
Related
I am trying to compile a multithreading program written in C++ running on WSL.
Here's what it looks like:
#include <thread>
void f() {}
int main()
{
std::thread t(f);
t.join();
return 0;
}
When trying to compile this I receive this error:
In file included from /usr/include/c++/9/chrono:40,
from /usr/include/c++/9/thread:38,
from test.cpp:1:
/usr/include/c++/9/limits:1665:7: internal compiler error: Segmentation fault
1665 | min() _GLIBCXX_USE_NOEXCEPT { return __FLT_MIN__; }
| ^~~
0x7f7d7a46b08f ???
/build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/x86_64/sigaction.c:0
0x7f7d7a44c082 __libc_start_main
../csu/libc-start.c:308
I discovered the issue in the <chrono> header as this receives the same error:
#include <chrono>
int main()
{
return 0;
}
I have tried compiling with -pthread -std=c++17. Additionally, I have tried updating g++ along with uninstalling it and reinstalling. None of this had any effect and the same error persisted.
#include <iostream>
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main() {
thread t(thread_c);
t.join();
return 0;
}
Such simple example, but it doesn't work on my g++ (MinGW.org GCC-6.3.0-1) 6.3.0, Windows 8.1
Here's the error:
main.cpp: In function 'int main()':
main.cpp:13:5: error: 'thread' was not declared in this scope
thread t(thread_c);
^~~~~~
main.cpp:14:5: error: 't' was not declared in this scope
t.join();
^
*I've been working on a big project, and noticed about some compilation errors. I wrote this code for testing my compiler out
What's wrong with it?
Loking at your code and the compiler results I see the following:
#include <thread> is not including a file that defines thread as your code expects
GCC 6.3.0 is very old (current is 10.2.0), and chances are it was built without POSIX thread support (and probably only support for Windows threads)
I always recommend using MinGW-w64 instead of MinGW as it is more up to date and supports both 32-bit and 64-bit Windows.
When I build your example with the MinGW-w64 from http://winlibs.com/ (which has POSIX thread support) I get no errors and the example works (output is 012345678910).
I would like to compile my c++11 project (recently moved to c++11) with MinGW. And I have compiling errors about c++11 code like "std::thread not found".
I used the last MinGW with gcc 5.3.0 (December 2015). A the end, I would like only to compile this example before to compile my big project :
#include <iostream>
#include <thread>
#include <chrono>
void foo()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
void bar()
{
// simulate expensive operation
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
std::cout << "starting first helper...\n";
std::thread helper1(foo);
std::cout << "starting second helper...\n";
std::thread helper2(bar);
std::cout << "waiting for helpers to finish..." << std::endl;
helper1.join();
helper2.join();
std::cout << "done!\n";
}
(source : http://en.cppreference.com/w/cpp/thread/thread/join)
I tried "g++ -std=c++11 main.cpp" and "g++ main.cpp -std=c++0x" but I have always those following errors :
main.cpp: In function 'void foo()':
main.cpp:8:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'void bar()':
main.cpp:14:10: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::seconds(1));
^
main.cpp: In function 'int main()':
main.cpp:20:5: error: 'thread' is not a member of 'std'
std::thread helper1(foo);
^
main.cpp:23:5: error: 'thread' is not a member of 'std'
std::thread helper2(bar);
^
main.cpp:26:5: error: 'helper1' was not declared in this scope
helper1.join();
^
main.cpp:27:5: error: 'helper2' was not declared in this scope
helper2.join();
^
MinGW mostly does not have a port of glibc which supports pthreading or gthreading like in GCC.
For solving this, the first solution can be installing library of thread headers.
The other solution can be working with GCC compiler.
Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags
I compiled & installed gcc4.4 using macports.
When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:
#include <thread>
...
std::thread t(handle);
t.join();
....
The compiler returns:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
But std::cout <<... compiles fine..
Can anyone help me?
gcc does not fully support std::thread yet:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Use boost::thread in the meantime.
Edit
Although the following compiled and ran fine for me with gcc 4.4.3:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
Compiled with
g++ -Wall -g -std=c++0x -pthread main.cpp
Output of a.out:
Printing from another thread
Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?
I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threads Including
mingw.mutex.h, mingw.thread.h files to global MinGW directory fixed this issue. All I had to do is to include header file and my code stayed the same
#include "mingw.thread.h"
...
std::thread t(handle);
...
Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.