为什么我在包含 -lrt 时仍然得到对 timer_create() 的未定义引用?

Why am i still getting an undefined reference to timer_create() when i have included -lrt?

marteli@marteli-Aspire-E5-575G:~/Downloads/Lab06$ make all
gcc -c -g -Wall -o scheduler.o scheduler.c
scheduler.c: In function ‘update_run_time’:
scheduler.c:47:37: warning: passing argument 2 of ‘clock_gettime’ from incompatible pointer type [-Wincompatible-pointer-types]
  if (clock_gettime( CLOCK_REALTIME, &final)== -1)
                                     ^
In file included from scheduler.c:6:0:
/usr/include/time.h:342:12: note: expected ‘struct timespec *’ but argument is of type ‘struct timespec **’
 extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
            ^
scheduler.c: In function ‘update_wait_time’:
scheduler.c:71:37: warning: passing argument 2 of ‘clock_gettime’ from incompatible pointer type [-Wincompatible-pointer-types]
  if (clock_gettime( CLOCK_REALTIME, &final)== -1)
                                     ^
In file included from scheduler.c:6:0:
/usr/include/time.h:342:12: note: expected ‘struct timespec *’ but argument is of type ‘struct timespec **’
 extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
            ^
scheduler.c: In function ‘main’:
scheduler.c:375:3: warning: implicit declaration of function ‘print_help’ [-Wimplicit-function-declaration]
   print_help(argv[0]);
   ^
gcc -c -g -Wall -o worker.o worker.c
gcc -c -g -Wall -o list.o list.c
list.c: In function ‘print_list’:
list.c:90:19: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
   printf("0x%X,", (unsigned int)le->info);
                   ^
gcc -g -lpthread -lrt -Wall -o scheduler scheduler.o worker.o list.o
scheduler.o: In function `init_sched_queue':
/home/marteli/Downloads/Lab06/scheduler.c:89: undefined reference to `sem_init'
/home/marteli/Downloads/Lab06/scheduler.c:96: undefined reference to `timer_create'
scheduler.o: In function `resume_worker':
/home/marteli/Downloads/Lab06/scheduler.c:112: undefined reference to `pthread_kill'
scheduler.o: In function `cancel_worker':
/home/marteli/Downloads/Lab06/scheduler.c:126: undefined reference to `pthread_kill'
scheduler.o: In function `suspend_worker':
/home/marteli/Downloads/Lab06/scheduler.c:169: undefined reference to `pthread_kill'
scheduler.o: In function `clean_up':
/home/marteli/Downloads/Lab06/scheduler.c:287: undefined reference to `sem_destroy'
scheduler.o: In function `create_workers':
/home/marteli/Downloads/Lab06/scheduler.c:312: undefined reference to `pthread_create'
/home/marteli/Downloads/Lab06/scheduler.c:316: undefined reference to `pthread_detach'
scheduler.o: In function `scheduler_run':
/home/marteli/Downloads/Lab06/scheduler.c:341: undefined reference to `timer_settime'
scheduler.o: In function `start_scheduler':
/home/marteli/Downloads/Lab06/scheduler.c:356: undefined reference to `pthread_create'
scheduler.o: In function `main':
/home/marteli/Downloads/Lab06/scheduler.c:375: undefined reference to `print_help'
/home/marteli/Downloads/Lab06/scheduler.c:383: undefined reference to `print_help'
/home/marteli/Downloads/Lab06/scheduler.c:409: undefined reference to `pthread_join'
worker.o: In function `cancel_thread':
/home/marteli/Downloads/Lab06/worker.c:16: undefined reference to `sem_post'
worker.o: In function `enter_scheduler_queue':
/home/marteli/Downloads/Lab06/worker.c:50: undefined reference to `sem_wait'
worker.o: In function `start_worker':
/home/marteli/Downloads/Lab06/worker.c:95: undefined reference to `pthread_sigmask'
/home/marteli/Downloads/Lab06/worker.c:105: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'scheduler' failed
make: *** [scheduler] Error 1
marteli@marteli-Aspire-E5-575G:~/Downloads/Lab06$ ^C
marteli@marteli-Aspire-E5-575G:~/Downloads/Lab06$ make all
gcc -g -pthread -lrt -Wall -o scheduler scheduler.o worker.o list.o
scheduler.o: In function `init_sched_queue':
/home/marteli/Downloads/Lab06/scheduler.c:96: undefined reference to `timer_create'
scheduler.o: In function `scheduler_run':
/home/marteli/Downloads/Lab06/scheduler.c:341: undefined reference to `timer_settime'
scheduler.o: In function `main':
/home/marteli/Downloads/Lab06/scheduler.c:375: undefined reference to `print_help'
/home/marteli/Downloads/Lab06/scheduler.c:383: undefined reference to `print_help'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'scheduler' failed
make: *** [scheduler] Error 1

包含所有未定义引用的第一次编译是在 makefile 运行 -lpthread.当我将 -lpthread 更改为 -pthread 时,我得到了更好的结果,但我仍然得到未定义的引用。我找不到 "print_help" 的任何内容,但我知道 -lrt 应该可以解决我现在遇到的问题,但您可以看到它已经包含在 make 中。 编辑:修复 clock_gettime() 的错误后,我编译,这是新日志

marteli@marteli-Aspire-E5-575G:~/Downloads/Lab06$ make all
gcc -c -g -Wall -o scheduler.o scheduler.c
scheduler.c: In function ‘main’:
scheduler.c:375:3: warning: implicit declaration of function ‘print_help’ [-Wimplicit-function-declaration]
   print_help(argv[0]);
   ^
gcc -g -pthread -lrt -Wall -o scheduler scheduler.o worker.o list.o
scheduler.o: In function `init_sched_queue':
/home/marteli/Downloads/Lab06/scheduler.c:96: undefined reference to `timer_create'
scheduler.o: In function `scheduler_run':
/home/marteli/Downloads/Lab06/scheduler.c:341: undefined reference to `timer_settime'
scheduler.o: In function `main':
/home/marteli/Downloads/Lab06/scheduler.c:375: undefined reference to `print_help'
/home/marteli/Downloads/Lab06/scheduler.c:383: undefined reference to `print_help'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'scheduler' failed
make: *** [scheduler] Error 1

更新 2:省略 print_help 后,这里是终端

marteli@marteli-Aspire-E5-575G:~/Downloads/Lab06$ make all
gcc -c -g -Wall -o scheduler.o scheduler.c
gcc -g -pthread -lrt -Wall -o scheduler scheduler.o worker.o list.o
scheduler.o: In function `init_sched_queue':
/home/marteli/Downloads/Lab06/scheduler.c:96: undefined reference to `timer_create'
scheduler.o: In function `scheduler_run':
/home/marteli/Downloads/Lab06/scheduler.c:341: undefined reference to `timer_settime'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'scheduler' failed
make: *** [scheduler] Error 1

库必须在 编译器命令行依赖于它们的目标文件之后,而不是在开头。所以:

gcc -g -Wall -o scheduler scheduler.o worker.o list.o -lpthread -lrt 

除此之外,在您的程序运行之前,您需要更正(请参阅警告)源代码中的许多严重错误。