代码之家  ›  专栏  ›  技术社区  ›  ubiquibacon

编译C程序时gcc出错

  •  1
  • ubiquibacon  · 技术社区  · 15 年前

    我有一个自定义shell程序,其中包括 signal.h , unistd.h ,和 stdio.h . 我最初是在RedHat Enterprise中进行这项工作的(不确定具体是哪个版本,但不是太旧),我能够在我的程序上使用gcc,它编译得很好,运行得也很好。现在我把它移到了Ubuntu上,gcc给了我一些错误,第一个是 conflicting types for 'getline()' . 还有一些错误说 incompatible implicit declaration of built-in function strlen

    /* define a global input buffer */
    #include <signal.h>
    #include <unistd.h>
    #include <stdio.h>
    
    #define MAXARG 512
    #define MAXBUF 512
    #define BUFFER_SIZE 50
    #define MAX_COMMANDS 10
    char buffer [BUFFER_SIZE];
    static char *prompt = "MYSHELL>";
    static char inpbuf[MAXBUF];
    static char *arg[MAXARG+1];
    static char tokbuf[2*MAXBUF];
    static char *tok = tokbuf;
    char history[MAX_COMMANDS][MAXBUF];
    int cmd_num;
    
    void getline(void);
    
    void getline() {
    int length;
    
    length = read(0, inpbuf, MAXBUF);
    if (length == 0) {
        printf("\n");
        exit(0);
    }
    inpbuf[length] = '\0';
    
    }
    
    void processline() {
    char *ptr = inpbuf;
    int narg;
    for (narg=0;;) {    
        arg[narg] = tok;
        for (; *ptr == ' ' || *ptr == '\t'; ptr++)
            ;
        while(*ptr != ' ' && *ptr != '\t' && *ptr != '\n' && 
              *ptr != '\0' && *ptr != ';' && *ptr != '&') 
            *tok++ = *ptr++;
        *tok++ = '\0';
        if (narg < MAXARG)
            narg++;
        if (*ptr == '\n')
            break;
    }
    // clear the input buffer
    for (ptr = inpbuf; *ptr != '\n'; ptr++)
        *ptr = ' ';
    if (narg != 0) {
        arg[narg] = NULL;
    }
    }
    
    void handle_SIGINT()
    {
    write(STDOUT_FILENO, buffer, strlen(buffer));
    }
    
    int main()
    {
        int pid, exitstat, ret;
        struct sigaction handler;
        handler.sa_handler = handle_SIGINT;
        handler.sa_flags = 0;
        sigemptyset(&handler.sa_mask);
        sigaction(SIGINT, &handler, NULL);
        strcpy(buffer, "Caught Control C\n");
    
        while (1) {
            printf("%s ", prompt);
            fflush(stdout);
            getline();
            processline();
            if ((pid = fork()) < 0){
                fprintf(stderr, "myshell: error\n");
                return (-1);
            }
    
            if (pid == 0) {
                execvp(*arg, arg);
                fprintf(stderr, "%s\n", *arg);
                exit(127);
            }
            waitpid(pid, &exitstat, 0);
        }
        return 0;
    }
    
    4 回复  |  直到 15 年前
        1
  •  4
  •   Kristian Glass adrianz    14 年前

    最简单的解决方案是重命名 getline() 功能,例如 my_getline()

        2
  •  4
  •   Prasoon Saurav    15 年前

    incompatible implicit declaration of built-in function strlen

    包括 <string.h>

    conflicting types for 'getline()

    <stdio.h> 已经包含getline的声明,所以请确保代码中没有您重新声明/重新定义的地方 getline() [使用不同的原型]。

        3
  •  0
  •   msw    15 年前
    gcc -Wall typoknig.c
    typoknig.c:19: error: conflicting types for ‘getline’
    //usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
    typoknig.c:21: error: conflicting types for ‘getline’
    //usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
    

    两个独立的声明 getline 安迪建议你用的 my_getline() stdio.h .


    typoknig.c:27:警告:函数退出的隐式声明
    typoknig.c:27:警告:内置函数退出的不兼容隐式声明

    man exit 上面写着:

    #include <stdlib.h>
    void exit(int status);
    

    stdlib.h ? gcc假设什么是未声明函数的签名?

    typoknig.c:在函数句柄中\u SIGINT:
    typoknig.c:59:警告:函数strlen的隐式声明
    typoknig.c:59:警告:内置函数strlen的隐式声明不兼容

    哎哟, man strlen 为了救援:

    #include <string.h>
    

    幸运的是, string.h exit :

    typoknig.c:在函数main中:
    typoknig.c:70:警告:函数strcpy的隐式声明
    typoknig.c:70:警告:内置函数strcpy的不兼容隐式声明
    typoknig.c:85:警告:内置函数出口的隐式声明不兼容

    预处理器不是很漂亮吗?

    typoknig.c:87: warning: implicit declaration of function ‘waitpid’
    typoknig.c:64: warning: unused variable ‘ret’
    

    赛耶斯 man waitpid

    #include <sys/types.h>
    #include <sys/wait.h>
    

    第64行留给读者作为练习。

        4
  •  0
  •   dckuehn sasha_gud    9 年前

    Getline() 不是C标准的一部分。它是GNU扩展。在c中++ 是标准的。

    #define _GNU_SOURCE
    

    应该修复警告。另请参见linux上的“man getline”。