简体   繁体   English

编译C程序时出现gcc错误

[英]Errors with gcc when compiling C program

I have a custom shell program in which I have included signal.h , unistd.h , and stdio.h . 我有一个自定义的shell程序,其中包含signal.hunistd.hstdio.h I was originally working on this in RedHat Enterprise (not sure exactly what version, but not too old) and I was able to use gcc on my program and it compiled fine and ran fine. 我最初是在RedHat Enterprise中进行此工作的(不确定确切的版本,但不太旧),并且能够在程序上使用gcc,并且可以正常运行。 Now I moved it over to Ubuntu and gcc is giving me some errors, the first of which is conflicting types for 'getline()' . 现在,我将其移至Ubuntu,gcc给了我一些错误,第一个错误是conflicting types for 'getline()' Some other errors say incompatible implicit declaration of built-in function strlen . 其他一些错误则表示incompatible implicit declaration of built-in function strlen I have overridden the functions in question, why was this working in RedHat but not in Ubuntu? 我已经覆盖了有问题的功能,为什么这在RedHat中起作用而在Ubuntu中却不起作用? Linux is not my thing so please speak plainly. Linux不是我的事,所以请坦白地说。 Let me know if you need more error details. 让我知道您是否需要更多错误详细信息。

/* 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;
}

incompatible implicit declaration of built-in function strlen

Include <string.h> 包括<string.h>

conflicting types for 'getline()

<stdio.h> already contains a declaration of getline, so make sure that nowhere in your code you have redeclared/redefined getline() [with a different prototype]. <stdio.h>已经包含getline的声明,因此请确保在代码中的任何地方都没有[使用不同的原型]重新声明/重新定义过getline()

最简单的解决方案是将您的getline()函数重命名,例如重命名为my_getline()

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

Two separate declarations of getline which Andy had recommended that you use my_getline() since the former is already part of stdio.h . Andy建议您使用my_getline()两个单独的getline声明,因为前者已经是stdio.h一部分。

typoknig.c: In function 'getline': typoknig.c:在“ getline”函数中:
typoknig.c:27: warning: implicit declaration of function 'exit' typoknig.c:27:警告:函数“ exit”的隐式声明
typoknig.c:27: warning: incompatible implicit declaration of built-in function 'exit' typoknig.c:27:警告:内置函数“ exit”的隐式声明不兼容

That can't be good, man exit says right at the top: 那不可能是好事, man exit在顶部说:

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

perhaps you need to include stdlib.h ? 也许您需要包括stdlib.h吗? What does gcc assume is the signature of an undeclared function? gcc假定未声明函数的签名是什么?

typoknig.c: In function 'handle_SIGINT': typoknig.c:在函数“ handle_SIGINT”中:
typoknig.c:59: warning: implicit declaration of function 'strlen' typoknig.c:59:警告:函数“ strlen”的隐式声明
typoknig.c:59: warning: incompatible implicit declaration of built-in function 'strlen' typoknig.c:59:警告:内置函数“ strlen”的隐式声明不兼容

Ouch, man strlen to the rescue: 哎呀, man strlen向救援man strlen了:

#include <string.h>

Fortunately, string.h will help out with the next one and we already nailed exit : 幸运的是, string.h将帮助解决下一个问题,我们已经确定了exit

typoknig.c: In function 'main': typoknig.c:在“ main”函数中:
typoknig.c:70: warning: implicit declaration of function 'strcpy' typoknig.c:70:警告:函数'strcpy'的隐式声明
typoknig.c:70: warning: incompatible implicit declaration of built-in function 'strcpy' typoknig.c:70:警告:内置函数'strcpy'的隐式声明不兼容
typoknig.c:85: warning: incompatible implicit declaration of built-in function 'exit' typoknig.c:85:警告:内置函数'exit'的隐式声明不兼容

Ain't that pre-processor nifty? 那个预处理器不是很漂亮吗?

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

Sayeth man waitpid : man waitpid

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

Line 64 is left as an exercise for the reader. 第64行留给读者练习。

Getline() is not part of C standard. Getline()不属于C标准。 It is a GNU extension. 它是GNU扩展。 In c++ Getline() is standard. 在c ++中, Getline()是标准的。

So adding to your code right before your #includes 因此,在#includes之前添加代码

#define _GNU_SOURCE

should fix the warning. 应该修正警告。 Also see "man getline" on linux. 另请参见Linux上的“ man getline”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM