1

Hello erveryone I get an not defined reference error everytime i complie my code.

Here is the code:

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

}

my handler is not done yet, so don`t mind it.

Makefile:

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

clean:  
    rm -rf nr1

And here the compiler message, I hope you don`t mind it is in german:

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

I looked through the board and couldn`t find a matching solution. So Iḿ hoping, that you can help me.

elhe
  • 122
  • 10

1 Answers1

1

You need to compile and link as:

gcc -Wall nr1.c -o nr1 -lrt

Read time(7) and timer_create(2). See also timerfd_create(2) & poll(2).

BTW, typing man timer_create would have given you the answer faster than asking here! See man(1).

Regarding your Makefile, get some inspiration here.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547