g++ does not know Sleep() when compiling on Linux
error: 'Sleep' was not declared in this scopeShort answer
Capital-S Sleep is a Windows function that takes milliseconds, so g++ on Linux cannot find it. Use std::this_thread::sleep_for from the thread header for portable C++, or sleep from unistd.h, which takes whole seconds. Remember the unit change when you convert the number.
Fix it fast
1Find out which cause you have
Look at the exact spelling and the includes in the file that fails. Capital S means the Windows call; lower case means a missing header.
grep -n -E "Sleep|sleep|#include" main.cpp2Match what you see, then jump to its fix
What does the error look like?
error: 'Sleep' was not declared in this scopeerror: 'sleep' was not declared in this scope
Cause 1 of 2
Sleep() is a Windows API, not part of Linux or standard C++
1Check
Microsoft documents Sleep(DWORD dwMilliseconds) for synchapi.h and Windows.h. Neither header exists on Linux, so nothing declares the name.
grep -n -i "windows.h" main.cpp2Fix
Use the standard C++ sleep, which works on Linux, macOS and Windows. The duration type carries the unit, so no manual conversion is needed.
#include <chrono>
#include <thread>
std::this_thread::sleep_for(std::chrono::milliseconds(500));3Verify
Compile with a C++11 or newer standard and run the program.
g++ -std=c++17 main.cpp -o app
./appConfirmed by en.cppreference.com
Cause 2 of 2
You call sleep() but did not include unistd.h
1Check
The POSIX function is declared in unistd.h and takes an unsigned int number of seconds. Without the include, the name is unknown. It also returns early when a signal arrives.
grep -n "unistd.h" main.cpp2Fix
Add the header, and pass seconds, not milliseconds. Use the standard C++ call from the first cause for sub-second waits.
#include <unistd.h>
sleep(2);3Verify
The file compiles and the pause is about two seconds.
g++ main.cpp -o app
time ./appConfirmed by man7.org
How do I stop it coming back?
- Prefer the standard library over platform calls so one source file builds on every OS.
- Build with warnings on (-Wall -Wextra) so missing headers surface early.
- When porting a Windows example, search it for Windows.h and any capitalised API names first.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
dnf cannot install MySQL on Amazon Linux 2023
dnf cannot find a mysql-server package on Amazon Linux 2023
Amazon Linux 2023 documents MariaDB, not MySQL, as its database server, with packages named mariadb105 and mariadb105-server.
3 causes, each with a fixServers
Ansible hangs, then times out waiting for the sudo prompt
a playbook using become seems to hang
Ansible ran sudo on the target and never saw the answer it expected.
3 causes, each with a fixServers
Apache on Ubuntu returns 404 Not Found for my site
The requested URL was not found on this server.
On Ubuntu a 404 from Apache usually means the request maps to a path that has no file.
3 causes, each with a fixServers
Apache returns 504 Gateway Timeout when PHP takes too long
Quick requests work, slow ones end in a 504 after a fixed wait
A 504 from Apache means the backend did not answer within the proxy timeout.
2 causes, each with a fixServers
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
Apache warns that a ${VAR} config variable is not defined
${VAR} left unchanged in the configuration, with a warning in the error log
Apache expands ${VAR} from Define or from the shell environment that exists when the server starts.
3 causes, each with a fix