我正在尝试在 Linux (Ubuntu 12.01 LTS) 中编译信号处理程序。这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
sigset_t ens1, ens2;
int sig;

int main(){
    /* Construction de l'ensemble ens1 = { SIGINT, SIGQUIT, SIGUSR1 }*/
    sigemptyset(&ens1);
    sigaddset(&ens1, SIGINT);
    sigaddset(&ens1, SIGQUIT);
    sigaddset(&ens1, SIGUSR1);
    sigprocmask(SIG_SETMASK, &ens1, NULL);  /* Installation du masque ens1 */
    /* Envoi à lui-même de deux exemplaires de SIGINT et un de SIGUSR1 */
    raise(SIGINT);
    kill(getpid(), SIGINT);
    kill(getpid(), SIGUSR1);
    /* Extraction des signaux pendants masqués */
    sigpending(&ens2);
    /* Impression de la liste des signaux pendants */
    printf("Signaux pendants: ");
    for(sig = 1; sig < NSIG; sig++){
        if(sigismember(&ens2, sig)) printf("%d ", sig);
        }
    putchar('\n');
    sleep(15);
    sigemptyset(&ens1);
    printf("Déblocage de tous les signaux\n");
    sigprocmask(SIG_SETMASK, &ens1, NULL);
    printf("Fin du processus\n");
    exit(0);
    }

问题是总是有如下错误:
gcc -o obj/signauxPendants.o -c src/Signal/signauxPendants.c -Iinclude -Wall -ansi
src/Signal/signauxPendants.c:18:1: error: unknown type name ‘sigset_t’

src/Signal/signauxPendants.c: In function ‘main’:
src/Signal/signauxPendants.c:23:2: warning: implicit declaration of function     ‘sigemptyset’ [-Wimplicit-function-declaration]

src/Signal/signauxPendants.c:24:2: warning: implicit declaration of function ‘sigaddset’ [-Wimplicit-function-declaration]

src/Signal/signauxPendants.c:27:2: warning: implicit declaration of function ‘sigprocmask’ [-Wimplicit-function-declaration]

src/Signal/signauxPendants.c:27:14: error: ‘SIG_SETMASK’ undeclared (first use in this function)
src/Signal/signauxPendants.c:27:14: note: each undeclared identifier is reported only once for each function it appears in

src/Signal/signauxPendants.c:30:2: warning: implicit declaration of function ‘kill’ [-Wimplicit-function-declaration]

src/Signal/signauxPendants.c:33:2: warning: implicit declaration of function ‘sigpending’ [-Wimplicit-function-declaration]

src/Signal/signauxPendants.c:36:21: error: ‘NSIG’ undeclared (first use in this function)

src/Signal/signauxPendants.c:37:3: warning: implicit declaration of function ‘sigismember’ [-Wimplicit-function-declaration]

make: *** [obj/signauxPendants.o] Error 1

最佳答案

signal.h 是一个 POSIX 库,不是标准的 C 库,去掉 -ansi 标志。

关于c - Linux 中无效的信号处理库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20849917/

10-11 18:28