std::this_thread is seen as undeclared

I was writing a slow-text output function that took use of C++ 11's `std::this_thread::sleep_for(std::chrono::milliseconds(delay));` I finished up the script and ran the command `g++ functions.cpp -std=c++11` and was greeted with this error message:
```
functions.cpp:29:14: error: 'std::this_thread' has not been declared
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
^~~~~~~~~~~
```
What makes this all the more strange, is that in my IDE(Visual Code Studio) I can right click the namespace to view it's declaration. I've tried several other help threads and have yet to come up with a result. If anyone has any ideas how to fix this issue your input would be much appreciated.
Please produce a complete, minimal example that reproduces the issue.

Did you #include <thread>?

________________________________

Upon further searching, I assume you are using MinGW (g++ on Windows).
Apparently (older?) versions of MinGW don't support threading right out of the box.
You might either need a more recent version of MinGW, or you need to make sure you have MinGW installed with threads.
See: https://stackoverflow.com/a/13785846/8690169

Personally, I use STL's MinGW distro, available at https://nuwen.net/mingw.html
It looks like it's had thread support since version 15.2 (2017).

If that is not possible, there are user-made headers you can download that reproduce the functionality that you need.
See: https://github.com/meganz/mingw-std-threads
This is a header-only library. To use, just include the corresponding mingw.xxx.h file, where xxx would be the name of the standard header that you would normally include.

For example, #include "mingw.thread.h" replaces #include <thread>.
Last edited on
Yes i am using `#include thread` also my MinGW is up to date and I've even dug through it's files and found where the thread library is.

Also I could not get the user made header to work
Last edited on
What's the output of "g++ -v"?
gcc version 8.2.0, I just reinstalled it
Wait does it say anything else?
Does --version give more information?

Is it 32-bit or 64-bit? If it's 32-bit, try a 64 bit version.

If that does not work, try the link in the SO link.

Edit: I'm still not getting a clear answer, did -v or --version show any other information?
Last edited on
Ok, I'll try the 64 Bit, though I don't see how that would influence it seeing the declaration of the namespace


Edit, where can I find a guide on installing mingw-w64, I might just be blind but I can't seem to find any guide on the sourceforge page and it is different than mingw-w32
Last edited on
I managed to install minGW-w64, however it still refuses to compile that file, while it works on other files. What is truly strange is that it is now giving me this as an error, but only when I try to compile the std::this_thread::sleep::for(std::chrono::milliseconds(delay));

C:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x2e):
undefined reference to `WinMain'
collect2.exe: error: ld returned 1 exit status
You keep making me play 20 questions. Are you trying to produce a program without a main() function? Just so we're absolutely clear, post exactly what the source code is, inside code tags (<> buttons on the edit screen)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

using std::string;

void slowcout(string text, unsigned int delay, bool flush) {
    int length = text.length();
    int num = -1;
    while(num ++<= length) {
        if(text[num] == '\\') {
            num ++;
            std::cout << '\\' << text[num];
        }else {
            std::cout << text[num];
        }
        num = num + 1;
        std::this_thread::sleep_for(std::chrono::milliseconds(delay));
    }
    if(flush == true) {
        std::cout << std::endl;
    }
}

This particular use does not have an int main() however even when I use it with an int main() it gives me the same issue

for clarification I am importing it to int main() via <#include "functions.cpp"> but even on it's own the issue still occurs
Last edited on
Thank you for providing a code example. I see what you're saying now.

I don't believe your version of MinGW supports threading with win32 threads. In fact, I don't think any current version of MinGW supports C++11 threads with win32 threads out of the box, so I was mistaken before.
https://stackoverflow.com/a/30390278/8690169

I believe this download is what is required for you to have <thread> support:
https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z/download

It has POSIX threading instead of Win32 threading.
I have actually not installed it yet. If you want to wait a few minutes, I will attempt to install it myself.

There a few paths you'll probably have to add as environmental variables (to your PATH env var).
mingw64\libexec\gcc\x86_64-w64-mingw32\8.1.0
mingw64\bin

Edit: I'm having trouble setting up all the proper include paths...
Edit 2: Okay I'm running into a bunch of issues actually getting it to work, but theoretically it should.
If I don't figure something out, what I suggest you do is search online for a MinGW package that is built with POSIX threading instead of Win32 threading.

Edit 3: I'm currently trying out the installer available at: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/

Ignore my previous link.
Last edited on
for clarification I am importing it to int main() via <#include "functions.cpp"> but even on it's own the issue still occurs

Don't #include source files (i.e. .cpp files). #include is for header files. Just allow the linker to link the object files as normal.

From your error message, it looks as though you're trying to build a Windows GUI project, which is why the linker is expecting a WinMain() function. Try recreating your project as a vanilla, empty project, so that it expects a simple main() function as normal.
Last edited on
Okay, I got it to work... I think you should be able to replicate it.

(Edit: The specific issue with WinMain is the fact that you're trying to compile your source as a final program and not as an object file. If you add -c to your command, it will compile it as an individual object file instead. So before you follow these steps, simply try adding -c to your command.)

First, I removed any references in environmental variables to old versions of MinGW, if any.

Then, I downloaded and ran the installer available at https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/

In the installer, select the x86_64 architecture, and make sure it's posix threading.
I installed everything inside C:\mingwfix, but you can choose anywhere you want.

Run the installer. After the installer is finished, you'll need to run the mingw-w64.bat inside the installation directory. Once you're using the mingw cmd, you can cd to the path where your .cpp file is.

Then, run something like:
g++ -c slowcout.cpp
I was finally able to compile your file that way, with threading.

The mingw-w64.bat simply sets temporary environmental variables. You can probably add the equivalent paths to your PATH if you don't want to have to use that.
Last edited on
It worked! Thank you and sorry if I was a bit unclear
The mingw-w64.bat simply sets temporary environmental variables. You can probably add the equivalent paths to your PATH if you don't want to have to use that.

you can also set them to persist. Sometimes this helps (I havent used this tool, but cygwin pathed out lets me do in windows, without it, it only works in a console).
Last edited on
Robonics: No problem, I also made assumptions that were wrong :)
You actually helped me update my compiler, since the one I was using also wasn't 100% up-to-date.
Now the error messages have fancy color highlighting!
Registered users can post here. Sign in or register to post.