Search code examples
cinlineextern

can I make functions with unresolved dependency in C?


what is the SHORTEST or Easiest way to solve the following dependency problem. Given, I have to keep xx in a separate file.

file1.h

    static inline void xx(){
      yy();//yy is not defined in this file but defined in file2.c;
    }

file2.c

  #include "file1.h"
  void yy(){
     printf("Hello");
   }

    void main(){
      xx();
    }

Compiler error in file1 yy not defined.


Solution

  • A declaration is required but not a definition:

    // file1.h
    
    static inline void xx() {
        void yy(); // Just a declaration
    
        yy(); 
    }
    
    // file2.c
    
    #include "file1.h"
    void yy() {
        printf("Hello");
    }
    
    int main() { // void main is not legal C
        xx(); // Works fine.
    }