0

I am trying to compile a very simple C code in VS2013. I did a bit of Googling beforehand and I realised I need to follow certain steps for that e.g. change the compiler from Default to Compile As C Code in my project properties.

The problem is that when I compile following code:

#include <stdio.h>
main()
{
    printf("Hello World! \n");
    sleep(5);
}

I get these errors:

Error   1   error LNK2019: unresolved external symbol _sleep referenced in function _main
Error   2   error LNK1120: 1 unresolved externals

And if I change the code to:

#include <stdio.h>
#include <Windows.h>
main()
{
    printf("Hello World! \n");
    Sleep(5000);
}

Then it works fine.

How can I get my first code compiled successfully? I have some tested codes in C and I need to use them. If I couldn't resolve above issue, then I have to update all syntaxes.

Bababarghi
  • 571
  • 1
  • 4
  • 23

4 Answers4

2

How can I get my first code compiled successfully?

You can't (at least exactly with this code as you stated). The point is that <stdio.h> header does not provide declaration for sleep() and C standard requires, that every function needs to be declared before its call. It's as simple as that.

The second thing is that sleep() function, that you want is likely from POSIX (specifically from <unistd.h> header). However, Visual Studio 2013 is not POSIX-compliant. Thus, the solution is to use Sleep(), that is declared in <Windows.h> header or possibly some other alternative.

Grzegorz Szpetkowski
  • 36,988
  • 6
  • 90
  • 137
2

Sleep is not a C standard function.

If you are on a UNIX system, then include <unistd.h>. However, if you are on a windows system, it should be in <windows.h> (which is why your second version of the code compiles successfully.

If you want to use in both systems, try something like this (from here):

#ifdef __linux__ 
    //linux include code goes here
#elif _WIN32
    // windows include code goes here
#else

#endif
Community
  • 1
  • 1
Cacho Santa
  • 6,846
  • 6
  • 41
  • 73
1

sleep is a non-standard function:

  • On UNIX use <unistd.h>
  • On MS-Windows use <windows.h>

There is no way work around this besides writing your own code:

void sleep (int seconds)
{
    Sleep (seconds * 1000);  // Converting to milliseconds
}
The Brofessor
  • 1,008
  • 6
  • 15
  • For older MSVC, it will be `#include ` – wallyk Aug 04 '15 at 23:47
  • Thanks for the reply. I am running my code on Windows platform. Do I still need that header? I thought that's only needed on Unix platforms. Although, I get this error now: `Error 1 error C1083: Cannot open include file: 'unistd.h': No such file or directory` – Bababarghi Aug 04 '15 at 23:49
  • @cpp_prog Did you read my question properly? I already mentioned that ! Adding `Windows.h` requires me to swap `sleep(5)` with `Sleep(5000)`. The entire thread is about to find a workaround not to follow that path ! – Bababarghi Aug 04 '15 at 23:52
1

The sleep function is not declared in <stdio.h>. If you're on unix, use sleep() from unistd.h, or for windows, Sleep() from Winbase.h (windows.h). If you want to see how Sleep() works, you can see how is sleep implemented at OS level?.

If you want to do it without having to change sleep() to Sleep(), you could use a macro:

#define sleep(s) Sleep((s) * 1000)

Macros are bad, so you may not want to do this in production code, but the advantage of the macro is that it will be guaranteed to be inlined, which will result in faster code.

Community
  • 1
  • 1
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75