2014-11-16 21 views
1

你好erveryone每次我編寫我的代碼時,我都會收到未定義的引用錯誤。信號未定義引用'timer_create'C

下面是代碼:

#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); 

} 

我的處理程序尚未完成,所以不介意吧。

的Makefile:

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 

我通過板看起來和couldn`t找到匹配解。所以我希望,你可以幫助我。

+0

順便說一句,你可以'出口LANG = C LC_ALL = C'在SO –

回答

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

+0

張貼診斷之前,哈哈哈非常感謝你,就是這樣。我永遠不會找到答案。你是男人 – AronC

+0

試着找出RTFM和STFW的含義。 –

+0

對不起,我剛剛在手冊頁上看不到它。 – AronC