为什么当我包含-lrt时,仍然对timer_create()有未定义的引用?

[email protected]:~/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

[email protected]:~/Downloads/Lab06$ ^C

[email protected]:~/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()后,我编译,这是新的日志为什么当我包含-lrt时,仍然对timer_create()有未定义的引用?

[email protected]:~/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后,这里是终端

[email protected]:~/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 

除此之外,你有许多源严重的bug,你需要更正(见警告)之前,你的程序将工作的。

以上是 为什么当我包含-lrt时,仍然对timer_create()有未定义的引用? 的全部内容, 来源链接: utcz.com/qa/258884.html

回到顶部