C Beginner: Can't use delay() in a simple C pr

2019-09-14 01:15发布

问题:

test1.c

#include <stdio.h>

int main(void) {
  printf("test\n");
  delay(1000);
  printf("test2\n");
}

When I try to compile...

gcc test1.c -o test1
Undefined symbols for architecture x86_64:
  "_delay", referenced from:
      _main in ccUnw3tY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Certainly there is a lesson here in knowing your libraries and what linking is etc... What am I missing? I am trying to do this on OSX.

回答1:

There's no delay function in C, you have to use sleep or usleep depending on what OS you're on.



回答2:

What make you think there is a delay function. I dont see one in the osx docs. There is a sleep function

https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/sleep.3.html



回答3:

An alternative of delay in C for unix os is the sleep function : https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man3/sleep.3.html do something like :

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

int main(void) {
  printf("test\n");
  usleep(1000);
  printf("test2\n");
}

If you value is for 1000 microsecondes.



回答4:

The delay function works in Borland C compiler. You have to use the dos.h header file in order to use delay. Some other compilers like MinGW may not support this.