我对我得到的编译器警告以及如何解决它们感到有些困惑。以下是错误和相关代码片段:
cmds的声明(与大多数人相关):
23: static char **cmds[] = { cmd0, cmd1, cmd2, cmd3, cmd4 };
24: static int ncmds = sizeof(cmds) / sizeof(cmds[0]);
pipeline.c: In function âexec_nth_commandâ:
pipeline.c:41: warning: declaration of âncmdsâ shadows a global declaration
pipeline.c:24: warning: shadowed declaration is here
pipeline.c:41: warning: declaration of âcmdsâ shadows a global declaration
pipeline.c:23: warning: shadowed declaration is here
41: static void exec_nth_command(int ncmds, char ***cmds)
pipeline.c: In function âexec_pipe_commandâ:
pipeline.c:68: warning: declaration of âncmdsâ shadows a global declaration
pipeline.c:24: warning: shadowed declaration is here
pipeline.c:68: warning: declaration of âcmdsâ shadows a global declaration
pipeline.c:23: warning: shadowed declaration is here
68: static void exec_pipe_command(int ncmds, char ***cmds, Pipe output)
pipeline.c: In function âexec_pipelineâ:
pipeline.c:79: warning: declaration of âncmdsâ shadows a global declaration
pipeline.c:24: warning: shadowed declaration is here
pipeline.c:79: warning: declaration of âcmdsâ shadows a global declaration
pipeline.c:23: warning: shadowed declaration is here
79: static void exec_pipeline(int ncmds, char ***cmds)
pipeline.c:82: warning: ISO C90 forbids mixed declarations and code
82: pid_t pid;
pipeline.c: In function âerr_usageâ:
pipeline.c:141: warning: declaration of âusestrâ shadows a global declaration
pipeline.c:26: warning: shadowed declaration is here
26: static char const usestr[] = "[-f filename]";
141: static void err_usage(char const *usestr)
答案 0 :(得分:1)
警告表示您的本地名称掩盖了具有相同名称的其他变量。所以
pipeline.c:41: warning: declaration of âncmdsâ shadows a global declaration
ncmds
参数隐藏了static char **cmds[] =
参数,使参数无法访问。
解决这些问题的方法是简单地为param或变量选择一个不同的名称。我会更改您经常使用的名称,因此您无需更改尽可能多的代码。您也可以忽略该警告,但其原因在于,当稍后查看代码或在该函数中编写新内容时,您可能会意外地引用另一个,因为相同的名称可能会令人困惑
pipeline.c:82: warning: ISO C90 forbids mixed declarations and code
这是因为在较旧的C风格中,不允许在除示波器顶部之外的任何地方声明变量。实际上在实践中没有问题,事实上,通常最好将变量声明为尽可能接近使用点,所以我不会在这里更改代码。相反,如果可以,请尝试使用-std=c99
进行编译 - 标准的较新版本允许使用它。