gpt4 book ai didi

c - 未定义引用 'timer_create ' C 的信号

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:05 25 4
gpt4 key购买 nike

大家好,我每次编译代码时都会收到 undefined reference 错误。

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <time.h>
#include <string.h>
#include <unistd.h>

#define MESSWERTTIMER (SIGRTMAX)
#define MESSREIHENTIMER (SIGRTMAX-1)

static void handler(int signo, siginfo_t *siginfo, void *context){
if(signo==MESSWERTTIMER){

}else if(signo==MESSREIHENTIMER){

}else if(signo ==SIGINT){
//timer_delete(timerid2);
//timer_delete(timerid1);
puts("Messwerte anzeigen");
perror("CTRL+C cached!\n");
exit(1);
}
}
//eventuell zwei sigevents zwei timer!!!!!!!

int main(void){
struct sigaction siga;
struct sigevent sigev1,sigev2;
struct itimerspec tspec,tspec2;
timer_t timerid1,timerid2;
sigset_t mask;

memset(&siga,0,sizeof(siga));
sigemptyset(&siga.sa_mask);
siga.sa_flags=SA_SIGINFO;
siga.sa_sigaction = &handler;

if(sigaction(MESSWERTTIMER,&siga,NULL)==-1){
perror("sigaction for MESSWERTTIMER failed horribly\n");
exit(EXIT_FAILURE);
}

if(sigaction(MESSREIHENTIMER,&siga,NULL)==-1){
perror("sigaction for MESSREIHENTIMER failed horribly\n");
exit(EXIT_FAILURE);
}

sigaction(SIGINT,&siga,NULL);

//blocking timer signals temporarily
printf("blocking signals for a short time\n");
sigemptyset(&mask);
sigaddset(&mask,MESSWERTTIMER);
sigaddset(&mask,MESSREIHENTIMER);
if(sigprocmask(SIG_SETMASK,&mask,NULL)==-1){
perror("sigprocmask\n");
exit(EXIT_FAILURE);
}
//creating timers
sigev1.sigev_notify=SIGEV_SIGNAL;
sigev1.sigev_signo=MESSWERTTIMER;
sigev1.sigev_value.sival_ptr= &timerid1;

sigev2.sigev_notify=SIGEV_SIGNAL;
sigev2.sigev_signo=MESSREIHENTIMER;
sigev2.sigev_value.sival_ptr= &timerid2;

if(timer_create(CLOCK_REALTIME,&sigev1,&timerid1)==-1){
perror("timer create 1\n");
exit(EXIT_FAILURE);
}
if(timer_create(CLOCK_REALTIME,&sigev2,&timerid2)==-1){
perror("timer create 2\n");
exit(EXIT_FAILURE);
}

tspec.it_value.tv_sec =0 ;
tspec.it_value.tv_nsec= 100000;
tspec.it_interval.tv_sec= tspec.it_value.tv_sec;
tspec.it_interval.tv_nsec= tspec.it_value.tv_nsec;

tspec2.it_value.tv_sec = 1;
tspec2.it_value.tv_nsec= 10000;
tspec2.it_interval.tv_sec= tspec2.it_value.tv_sec;
tspec2.it_interval.tv_nsec= tspec2.it_value.tv_nsec;

//unblocking signals
if(sigprocmask(SIG_UNBLOCK,&mask,NULL)==-1){
perror("sigprocmask unblock\n");
exit(EXIT_FAILURE);
}

if(timer_settime(timerid1,0,&tspec,NULL)==-1){
perror("settime 1\n");
exit(EXIT_FAILURE);
}
if(timer_settime(timerid2,0,&tspec2,NULL)==-1){
perror("settime 2\n");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);

}

我的处理程序还没有完成,所以不要介意。

生成文件:

nr1:    nr1.c
gcc -Wall nr1.c -o nr1

clean:
rm -rf nr1

这里是编译器消息,我希望你不介意它是德语的:

gcc -Wall nr1.c -o nr1 
nr1.c:9:1: warning: "/*" within comment [-Wcomment]
/****************************************************************************/
^
/tmp/ccnoBsjh.o: In Funktion `main':
nr1.c:(.text+0x1f9): Nicht definierter Verweis auf `timer_create'
nr1.c:(.text+0x22d): Nicht definierter Verweis auf `timer_create'
nr1.c:(.text+0x2fc): Nicht definierter Verweis auf `timer_settime'
nr1.c:(.text+0x335): Nicht definierter Verweis auf `timer_settime'
collect2: error: ld returned 1 exit status
make: *** [nr1] Fehler 1

我翻遍了电路板,找不到匹配的解决方案。所以我希望你能帮助我。

最佳答案

您需要编译和链接为:

gcc -Wall nr1.c -o nr1 -lrt

阅读time(7)timer_create(2) .另见 timerfd_create(2) & poll(2) .

顺便说一句,输入 man timer_create 会比在这里询问更快地给您答案!参见 man(1) .

关于您的Makefile,获得一些灵感here .

关于c - 未定义引用 'timer_create ' C 的信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959660/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com