进行条件编译时出错

Error while making conditional compilation

[在此处输入 link 描述][1]我正在编写此程序,它将启用条件编译。

当程序以这样的标准方式编译时,它可以正常工作,没有任何错误消息:

gcc -Werror -Wall -pedantic -ansi -g -c display.c -o displayStack 

但是当我想用条件编译进行编译时,我有一条错误消息说我的函数在 display.c

中有隐式声明

以下是我的编译方式:

gcc -Werror -Wall -pedantic -ansi -g -c display.c -o displayStack -D STACK

下面是我在代码中编写条件命令的方式:

#ifdef STACK
printLinkedList( stack );
#endif

这里是 stack.h 文件,显示了 LinkedList.h 的包含,它具有 printLinkedList.

的函数原型
#ifndef STACK
#define STACK

#include "LinkedList.h"

LinkedList *createStack();
void push( LinkedList *, void * );
void *top( LinkedList * );
void *pop( LinkedList * );
void freeStack( LinkedList * );

#endif

请问这里有什么问题吗?我似乎找不到问题,因为第一条语句运行良好,但是当我添加“-D STACK”时,程序只显示错误消息。是不是我的编译命令有问题?

我收到的错误消息:

display.c: In function ‘display’:
display.c:21:5: error: unknown type name ‘LinkedList’
   21 |     LinkedList *stack = NULL;
      |     ^~~~~~~~~~
display.c:22:13: error: implicit declaration of function ‘createStack’ [-Werror=implicit-function-declaration]
   22 |     stack = createStack();
      |             ^~~~~~~~~~~
display.c:22:11: error: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   22 |     stack = createStack();
      |           ^
display.c:47:21: error: implicit declaration of function ‘push’ [-Werror=implicit-function-declaration]
   47 |                     push( stack, bracket );
      |                     ^~~~
display.c:52:30: error: request for member ‘head’ in something not a structure or union
   52 |                     if( stack->head != NULL )
      |                              ^~
display.c:54:38: error: implicit declaration of function ‘top’ [-Werror=implicit-function-declaration]
   54 |                         popBracket = top( stack );
      |                                      ^~~
display.c:54:36: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   54 |                         popBracket = top( stack );
      |                                    ^
display.c:72:42: error: implicit declaration of function ‘pop’ [-Werror=implicit-function-declaration]
   72 |                             popBracket = pop( stack );
      |                                          ^~~
display.c:72:40: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   72 |                             popBracket = pop( stack );
      |                                        ^
display.c:108:17: error: implicit declaration of function ‘printLinkedList’ [-Werror=implicit-function-declaration]
  108 |                 printLinkedList( stack );
      |                 ^~~~~~~~~~~~~~~
display.c:147:22: error: request for member ‘head’ in something not a structure or union
  147 |             if( stack->head == NULL )   /* Good case */
      |                      ^~
display.c:151:28: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
  151 |                 popBracket = top( stack );
      |                            ^
display.c:164:5: error: implicit declaration of function ‘freeStack’ [-Werror=implicit-function-declaration]
  164 |     freeStack( stack );
      |     ^~~~~~~~~
cc1: all warnings being treated as errors
calmen@calmen:~/Desktop/Project/BracketCheck$ vim display.h
calmen@calmen:~/Desktop/Project/BracketCheck$ gcc -Werror -Wall -pedantic -ansi -g -c display.c -o displayStack -D STACK
display.c: In function ‘display’:
display.c:21:5: error: unknown type name ‘LinkedList’
   21 |     LinkedList *stack = NULL;
      |     ^~~~~~~~~~
display.c:22:13: error: implicit declaration of function ‘createStack’ [-Werror=implicit-function-declaration]
   22 |     stack = createStack();
      |             ^~~~~~~~~~~
display.c:22:11: error: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   22 |     stack = createStack();
      |           ^
display.c:47:21: error: implicit declaration of function ‘push’ [-Werror=implicit-function-declaration]
   47 |                     push( stack, bracket );
      |                     ^~~~
display.c:52:30: error: request for member ‘head’ in something not a structure or union
   52 |                     if( stack->head != NULL )
      |                              ^~
display.c:54:38: error: implicit declaration of function ‘top’ [-Werror=implicit-function-declaration]
   54 |                         popBracket = top( stack );
      |                                      ^~~
display.c:54:36: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   54 |                         popBracket = top( stack );
      |                                    ^
display.c:72:42: error: implicit declaration of function ‘pop’ [-Werror=implicit-function-declaration]
   72 |                             popBracket = pop( stack );
      |                                          ^~~
display.c:72:40: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
   72 |                             popBracket = pop( stack );
      |                                        ^
display.c:108:17: error: implicit declaration of function ‘printLinkedList’ [-Werror=implicit-function-declaration]
  108 |                 printLinkedList( stack );
      |                 ^~~~~~~~~~~~~~~
display.c:147:22: error: request for member ‘head’ in something not a structure or union
  147 |             if( stack->head == NULL )   /* Good case */
      |                      ^~
display.c:151:28: error: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Werror=int-conversion]
  151 |                 popBracket = top( stack );
      |                            ^
display.c:164:5: error: implicit declaration of function ‘freeStack’ [-Werror=implicit-function-declaration]
  164 |     freeStack( stack );
      |     ^~~~~~~~~
cc1: all warnings being treated as errors

文件可以在此处的文件夹中找到。 [1]: https://github.com/Calmen00-code/BracketCheck

gcc -Werror -Wall -pedantic -ansi -g -c display.c -o displayStack -D STACK

很奇怪。我会删除 -DSTACK

之间的 space

您可能会对在 gcc 命令中添加 -Wextra -fanalyze 感兴趣。阅读有关 invoking GCC, section about static analyzer options, and in end of 2020 consider upgrading to GCC 10.

的章节

此外,-ansi 指的是旧的 C 标准。阅读 n1570 and Modern C 然后考虑使用 -std=c11

当使用 make CFLAGS='-Wall -Wextra -g -std=c11' 编译代码 BracketCheck.tar.gz(md5sum 1b81b34f26db85fef69bda62a5bd4e63)时,我得到:

 display.c: In function ‘display’:
 display.c:21:5: error: unknown type name ‘LinkedList’
    21 |     LinkedList *stack = NULL;
       |     ^~~~~~~~~~
 display.c:22:13: warning: implicit declaration of function ‘createStack’ [-Wimplicit-function-declaration]
    22 |     stack = createStack();
       |             ^~~~~~~~~~~
 display.c:22:11: warning: assignment to ‘int *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    22 |     stack = createStack();
       |           ^
 display.c:47:21: warning: implicit declaration of function ‘push’ [-Wimplicit-function-declaration]
    47 |                     push( stack, bracket );
       |                     ^~~~
 display.c:52:30: error: request for member ‘head’ in something not a structure or union
    52 |                     if( stack->head != NULL )
       |                              ^~
 display.c:54:38: warning: implicit declaration of function ‘top’ [-Wimplicit-function-declaration]
    54 |                         popBracket = top( stack );
       |                                      ^~~
 display.c:54:36: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    54 |                         popBracket = top( stack );
       |                                    ^
 display.c:72:42: warning: implicit declaration of function ‘pop’ [-Wimplicit-function-declaration]
    72 |                             popBracket = pop( stack );
       |                                          ^~~
 display.c:72:40: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
    72 |                             popBracket = pop( stack );
       |                                        ^
 display.c:108:17: warning: implicit declaration of function ‘printLinkedList’ [-Wimplicit-function-declaration]
   108 |                 printLinkedList( stack );
       |                 ^~~~~~~~~~~~~~~
 display.c:147:22: error: request for member ‘head’ in something not a structure or union
   147 |             if( stack->head == NULL )   /* Good case */
       |                      ^~
 display.c:151:28: warning: assignment to ‘char *’ from ‘int’ makes pointer from integer without a cast [-Wint-conversion]
   151 |                 popBracket = top( stack );
       |                            ^
 display.c:164:5: warning: implicit declaration of function ‘freeStack’ [-Wimplicit-function-declaration]
   164 |     freeStack( stack );

这是在 Linux/x86-64/Ubuntu 20.04 上 GCC 10.2

我建议修复你的代码,直到你完全没有收到警告。

添加 -DSTACK 并不能修复所有错误。一旦它们全部修复,请使用 GDB 了解您的程序的行为。

您可能会对 this draft report and the DECODER and CHARIOT 个资助它的项目感兴趣。

您也可以尝试在您的代码上使用 Frama-C or the Clang static analyzer. Be aware of Rice's theorem

您可以考虑使用 GNU autoconf and/or GNU bison and/or ANTLR and/or GPP to generate some of your code (in C, or your makefile ...) using meta-programming techniques after reading the Dragon book

问题是 stack.h 文件中的 include guard:

#ifndef STACK

当您随后在命令行上定义 STACK 时,条件变为 FALSE,并且不包括 header 内容。这会导致 C 文件中缺少原型。

要解决此问题,您需要更改 C 文件或 header 文件以使用不同的预处理器变量。