Search code examples
cconditional-compilationmakefile

Error while making conditional compilation


[enter link description here][1]I am writing this program which will enabled conditional compilation.

When the program is compiled in a standard way like this, it works fine without any error message:

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

But when I would like to compile with conditional compilation, I have an error message saying that there are implicit declaration of my functions in display.c

Here is how i compile it:

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

Here is how I wrote the conditional command in the code:

#ifdef STACK
printLinkedList( stack );
#endif

Here is the stack.h file showing the include of LinkedList.h which has the function prototype for printLinkedList.

#ifndef STACK
#define STACK

#include "LinkedList.h"

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

#endif

May I know is there any issue over here? I cannot seem to find the problem because the first statement was working perfectly but when I have added "-D STACK" the program just show error message. Is it something wrong with my compiling command?

Error message I am receiving:

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

The files can be find in the folder here. [1]: https://github.com/Calmen00-code/BracketCheck


Solution

  • The problem is the include guard in the stack.h file:

    #ifndef STACK
    

    When you then define STACK on the command line that condition becomes FALSE and the header contents are not included. This results in missing prototypes in the C file.

    To fix this you need to change either the C files or the header file to use a different preprocessor variable.