title: Creating a portable library to run on both linux and windows
body: <pre><code>gcc (GCC) 4.7.2
  </code></pre>
  
  <p>Hello,</p>
  
  <p>I am creating a shared library that will compile on linux and a dll that will compile on windows using the same source code. So i am creating an portable library for both linux and windows.</p>
  
  <p>In my header file for the library is this i.e. module.h</p>
  
  <pre><code>#ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifdef _WIN32
  #define LIB_INTERFACE(type) EXTERN_C __declspec(dllexport) type
  #else
  #define LIB_INTERFACE(type) type
  #endif
  
  LIB_INTERFACE(int) module_init();
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>In the source I have the following i.e. module.c</p>
  
  <pre><code>#include "module.h"
  
  LIB_INTERFACE(int) module_init()
  {
      /* do something useful
      return 0;
  }
  </code></pre>
  
  <p>And in my test application that will link and use this module.so I have this:</p>
  
  <pre><code>#include "module.h"
  
  int main(void)
  {
      if(module_init() != 0) {
      return -1;
      }
      return 0;
  }
  </code></pre>
  
  <p>1) Is what I have done above is it a correct implementation of creating a portable library for linux and windows?</p>
  
  <p>2) I am just wondering as I have wrapped the functions in <code>extern "C"</code> so that this library can been called from a program that has been compiled in C++. Do I still need this <code>EXTERN_C</code> in the following:</p>
  
  <pre><code>#define LIB_INTERFACE(type) EXTERN_C __declspec(dllexport) type
  </code></pre>
  
  <p>3) What is the purpose of the <code>EXTERN_C</code>?</p>
  
  <p>Many thanks in advance,</p>