2

I'm using GNU GCC compiler in Code::Blocks on Windows. Why doesn't sleep(seconds) work here? I've tried it using library and it works fine. Thanks.

Edit: By "doesn't work" I mean, doesn't compile. Sorry.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]){
    char * c = (char *) malloc(sizeof(char)*50);
    if(--argc>0){
        printf("POTATO: \n");
        while(argc>=1){
            printf("- %s\n", argv[argc]);
            sleep(10);
            argc--;
        }
        printf("\n");
    }

    printf("A\n");
    scanf("%s", c);
    printf("What you wrote: %s\n", c);
    scanf("%s", c);
    return 0;
}
SadSeven
  • 1,247
  • 1
  • 13
  • 20

2 Answers2

4

Assuming "doesn't work means "doesn't compile":

sleep() is IX'ish. The appropriate win32 call would be Sleep(). The difference is that the latter takes ms, but s, as sleep() does.


To have portable code writing your own wrapper like this: https://stackoverflow.com/a/14818830/694576 might help.

Community
  • 1
  • 1
alk
  • 69,737
  • 10
  • 105
  • 255
  • It doesn't compile I think: "undefined reference to 'sleep'" I know about that ms and s difference, my question is: If I'm using the same compiler and library as in linux why doesn't it work... – SadSeven Feb 16 '14 at 11:25
  • 1
    @SadSeven: The win32's `Sleep()` starts with a capital `S`. – alk Feb 16 '14 at 11:29
  • 2
    @SadSeven It does not work because there is no sleep() function in windows. There is a Sleep() function (with a capital S), but it takes miliseconds instead of seconds. – nos Feb 16 '14 at 11:30
  • 1
    @SadSeven The compiler doesn't matter. It is the library that matters. And unless you get a library with the same interface as you have under linux, it just won't work. – glglgl Feb 16 '14 at 11:33
  • I know that guys, that's not the point lol. The one with is working fine. I don't understand why doesn't the work (compile). – SadSeven Feb 16 '14 at 11:33
  • You're using the wrong compiler suite, or the wrong functionality (can look at it from either side). The header `unistd.h` contains, as the name would suggest, "unix standard" functions, i.e. POSIX functions. Windows is not POSIX compliant, and MinGW _explicitly does not attempt to_ provide a complete POSIX compatibility layer. If this is what you want, use Cygwin. – Damon Feb 16 '14 at 13:14
  • @Damon: Your comment was addressed to *SadSeven*, wasn't it? – alk Feb 16 '14 at 13:22
  • @alk: Yes, I did not @ the OP since I thought it was obvious, and I was under the impression that question OP _always_ gets mailboxed, is that not true? – Damon Feb 16 '14 at 13:45
  • @Damon: I think only the answerer is notified. But you are right, the questioner also might be. Anybody correct me if I'm wrong, I cannot remember, as posing to few questions ... :-} (it "*was obvious* to me btw, yes) – alk Feb 16 '14 at 13:55
  • @SadSeven There are many portability issues, this is one of them. See here for a solution https://www.mail-archive.com/wsjt-devel@lists.berlios.de/msg01067.html . For a more comprehensive solution there is Apache Portable Runtime (for example) which provides wrappers for many functions which have potential portability problems such as sleep, timezones, directory listings, character encodings etc – Brandin Feb 16 '14 at 14:10
0

because command line argument argc is 1. so if condition is not working, use print statement before if() condition.