What does extern inline do?

后端 未结 6 2129
闹比i
闹比i 2020-11-22 12:07

I understand that inline by itself is a suggestion to the compiler, and at its discretion it may or may not inline the function, and it will also produce linkab

6条回答
  •  抹茶落季
    2020-11-22 12:17

    Macros are your choice here rather than the inline functions. A rare occasion where macros rule over inline functions. Try the following: I wrote this "MACRO MAGIC" code and it should work! Tested on gcc/g++ Ubuntu 10.04

    //(c) 2012 enthusiasticgeek (LOGGING example for StackOverflow)
    
    #ifdef __cplusplus
    
    #include 
    #include 
    
    #else
    
    #include 
    #include 
    
    #endif
    
    //=========== MACRO MAGIC BEGINS ============
    
    //Trim full file path
    #define __SFILE__ (strrchr(__FILE__,'/') ? strrchr(__FILE__,'/')+1 : __FILE__ )
    
    #define STRINGIFY_N(x) #x
    #define TOSTRING_N(x) STRINGIFY_N(x)
    #define _LINE (TOSTRING_N(__LINE__))
    
    #define LOG(x, s...) printf("(%s:%s:%s)"  x "\n" , __SFILE__, __func__, _LINE, ## s);
    
    //=========== MACRO MAGIC ENDS ============
    
    int main (int argc, char** argv) {
    
      LOG("Greetings StackOverflow! - from enthusiasticgeek\n");
    
      return 0;
    }
    

    For multiple files define these macros in a separate header file including the same in each c/cc/cxx/cpp files. Please prefer inline functions or const identifiers (as the case demands) over macros wherever possible.

提交回复
热议问题