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

2019-09-14 00:53发布

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.

4条回答
Bombasti
2楼-- · 2019-09-14 01:25

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

查看更多
倾城 Initia
3楼-- · 2019-09-14 01:27

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

查看更多
走好不送
4楼-- · 2019-09-14 01:28

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.

查看更多
smile是对你的礼貌
5楼-- · 2019-09-14 01:38

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.

查看更多
登录 后发表回答