如何在没有警告的情况下编译程序?

时间:2015-05-10 07:12:13

标签: c linux gcc compilation compiler-warnings

我应该用spec标志编译我的程序到gcc。然后gcc抱怨没有处理的返回值。当我使用变量获取返回值时,gcc再次抱怨:

$ gcc -pedantic -Wall -ansi -O4 miniShell.c
miniShell.c: In function ‘cd’:
miniShell.c:108:9: warning: variable ‘other_return’ set but not used [-Wunused-but-set-variable]
     int other_return;
         ^
miniShell.c:107:12: warning: variable ‘return_value’ set but not used [-Wunused-but-set-variable]
     char * return_value;
            ^

如何解决警告?我的计划如下。

#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

#define BUFFER_LEN 1024
#define BUFFERSIZE 1024


int mystrcmp(char const *, char const *);


void err_syserr(char *fmt, ...)
{
    int errnum = errno;
    va_list args;
    va_start(args, fmt);
    vfprintf(stderr, fmt, args);
    va_end(args);
    if (errnum != 0)
        fprintf(stderr, "(%d: %s)\n", errnum, strerror(errnum));
    exit(EXIT_FAILURE);
}
int main() {
    char line[BUFFER_LEN];  
    char* argv[100];        
    char* path= "/bin/";    
    char progpath[20];      
    int argc;               
    size_t length;
    char *token;
    int i=0;
    int pid;
    while(1) {
    i = 0;
        printf("miniShell>> ");                    

        if(!fgets(line, BUFFER_LEN, stdin)) { 
            break;                                
        }
        length = strlen(line);
        if (line[length - 1] == '\n') {
            line[length - 1] = '\0';
        }
        if(strcmp(line, "exit")==0) {           
            break;
        }


        token = strtok(line," ");

        while(token!=NULL) {
            argv[i]=token;
            token = strtok(NULL," ");
            i++;
        }
        argv[i]=NULL;                     

        argc=i;                           
        for(i=0; i<argc; i++) {
            printf("%s\n", argv[i]);      
        }
        strcpy(progpath, path);           
        strcat(progpath, argv[0]);            

        for(i=0; i<strlen(progpath); i++) {   
            if(progpath[i]=='\n') {
                progpath[i]='\0';
            }
        }
        pid= fork();              

        if(pid==0) {              
            execvp(progpath,argv);
            fprintf(stderr, "Child process could not do execvp\n");

        } else {                  
            wait(NULL);
            printf("Child exited\n");
        }

    }
return (0);
}

int mystrcmp(char const *p, char const *q)
{
    int i = 0;
    for(i = 0; q[i]; i++)
    {
        if(p[i] != q[i])
            return -1;
    }
    return 0;
}

int cd(char *pth) {
    char path[BUFFERSIZE];
    char cwd[BUFFERSIZE];
    char * return_value;
    int other_return;
    strcpy(path,pth);

    if(pth[0] != '/')
    {  
        return_value = getcwd(cwd,sizeof(cwd));
        strcat(cwd,"/");
        strcat(cwd,path);
        other_return = chdir(cwd);
    } else { 
        other_return = chdir(pth);
    }
    printf("Spawned foreground process: %d\n", getpid());
    return 0;
}

3 个答案:

答案 0 :(得分:3)

在线之间阅读,我想你试图解决的原始问题是一个警告:

 warning: ignoring return value of ‘chdir’

您已尝试通过将返回值分配给变量(其本身现在未使用)来解决此问题。

如果错误代码失败,

getcwdchdir都会返回错误代码,这是GCC警告您的返回值。如果要正确修复警告,则应在代码中添加逻辑以检测和处理这些错误情况。否则,您可能会继续处理与您的假设不一致的状态(例如,如果getcwd失败并且您的缓冲区处于错误初始化状态,则可能处于意外目录中。)

我原本以为可以通过将函数调用的结果转换为void来覆盖它,但这不起作用(你仍然可以玩弄技巧,但它们会变得混乱!)。 warn_unused_result属性的GCC文档说:

  

warn_unused_result属性会导致发出警告   具有此属性的函数的调用者不使用其返回值   值。这对于不检查结果的函数很有用   无论是安全问题还是总是错误,

这表明您不希望找到警告的解决方法,并且应该检查错误条件的返回值。

如果您真的想这样做,请将结果分配给变量,然后添加该变量的一次使用,强制转换为void:

int res = chdir (cwd);
/* I promise I don't need to check this return value.  */
(void) res;

答案 1 :(得分:1)

您的代码中实际上有两个错误。第一个可能导致您更改代码,然后引起您现在要求的警告。

我打赌你的第一个初步警告是chdir。正如文件所说:

  

man chdir(3):“成功完成后,将返回0。                  否则,返回-1,当前正在工作                  目录应保持不变,并且errno应设置为                  表示错误。“

正如它所说,chdir可以返回错误代码,说明是否出现任何问题,并且由于您只是忽略该值而导致第一个初始警告。

然后您更改了代码并将值分配给另一个变量并得到:

  

警告:变量'other_return'设置但未使用[-Wunused-but-set-variable]    int other_return;

你可以在这个函数中看到你只是为那个变量设置一个值,但是之后实际上没有使用它,这意味着你可以删除它:

int cd(char *pth) {
char path[BUFFERSIZE];
char cwd[BUFFERSIZE];
char * return_value;
int other_return;
strcpy(path,pth);

if(pth[0] != '/')
{  
    return_value = getcwd(cwd,sizeof(cwd));
    strcat(cwd,"/");
    strcat(cwd,path);
    other_return = chdir(cwd);
} else { 
    other_return = chdir(pth);
}
printf("Spawned foreground process: %d\n", getpid());
return 0;
}

return_value也是如此。你可以简单地删除它们,因为它们没有被使用。

如果您想避免第一次初始警告,您可以随时执行以下操作:

int res = chdir (cwd);
(void) res;

这是您对编译器保证一切正常并且可以忽略警告的保证。

答案 2 :(得分:1)

其他用户已经回答了如何通过修复代码来消除警告,但仅供参考:如果您想“忽略”警告,请使用标志进行编译:

-Wno-unused-but-set-variable

编译器通常会在警告结束时为您提供一个标记(在您的情况下,它是-Wunused-but-set-variable)。要忽略它,只需将-W更改为-Wno -

希望这有帮助!

相关问题