Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

g++ does not know Sleep() when compiling on Linux

Errorerror: 'Sleep' was not declared in this scope

Short 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.

  • Applies to g++ 13, Ubuntu 24.04, C++17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.cpp
  2. 2Match what you see, then jump to its fix

    1. 1the error names Sleep with a capital S, and the code includes windows.h or was copied from a Windows exampleSleep() is a Windows API, not part of Linux or standard C++Fix
    2. 2the error names sleep in lower caseYou call sleep() but did not include unistd.hFix

What does the error look like?

  • error: 'Sleep' was not declared in this scope
  • error: '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.cpp

2Fix

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
./app

Confirmed by en.cppreference.com

Did this fix it?

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.cpp

2Fix

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 ./app

Confirmed by man7.org

Did this fix it?

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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.