-3

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.

user7860670
  • 35,849
  • 4
  • 58
  • 84
sapy
  • 8,952
  • 7
  • 49
  • 60

3 Answers3

1

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.
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

Pre-Declare yy.

file1.h

+++ void yy();

    static inline void xx(){
      yy();
    }
datenwolf
  • 159,371
  • 13
  • 185
  • 298
1

Just function declaration prior using will solve the issue.

static inline void xx() {
    void yy();
    yy();  // no more yy is not declared
}
273K
  • 29,503
  • 10
  • 41
  • 64