title: How to Link Golang package to an existing C project (Using Go from C) on windows x86-64
body: <p>How to use Golang Generated <code>libgolang.a</code> from  C code to build C executable: <code>test.exe</code>:  </p>
  
  <p>these commands makes executable binary 'test' in Ubuntu x86-64 and works fine (but not in Windows x86-64):  </p>
  
  <pre><code>go build -buildmode c-archive -o libgolang.a
  gcc -o test _main.c libgolang.a -lpthread
  </code></pre>
  
  <p>with:<br>
  this is <code>main.go</code> file:  </p>
  
  <pre class="lang-golang prettyprint-override"><code>package main
  
  import "C"
  import "fmt"
  
  //export Add
  func Add(a, b uint64) uint64 {
      return a + b
  }
  
  func main() {
      fmt.Println("Hi")
  }
  </code></pre>
  
  <p>and this is <code>_main.c</code> file:  </p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdint.h&gt;
  #include "libgolang.h"
  
  int main()
  {
      uint64_t a=10;
      uint64_t b=20;
      uint64_t c=Add(a,b);
      printf("%ld\n",c);
      return 0;
  }
  </code></pre>
  
  <p>in Windows this command:  </p>
  
  <pre><code>go build -buildmode c-archive -o libgolang.a
  </code></pre>
  
  <p>works fine and generates <code>libgolang.a</code> and <code>libgolang.h</code> files. <code>libgolang.h</code>:</p>
  
  <pre class="lang-c prettyprint-override"><code>/* Created by "go tool cgo" - DO NOT EDIT. */
  
  /* package github.com/ARamazani/go/DLL */
  
  /* Start of preamble from import "C" comments.  */
  
  
  
  
  /* End of preamble from import "C" comments.  */
  
  
  /* Start of boilerplate cgo prologue.  */
  
  #ifndef GO_CGO_PROLOGUE_H
  #define GO_CGO_PROLOGUE_H
  
  typedef signed char GoInt8;
  typedef unsigned char GoUint8;
  typedef short GoInt16;
  typedef unsigned short GoUint16;
  typedef int GoInt32;
  typedef unsigned int GoUint32;
  typedef long long GoInt64;
  typedef unsigned long long GoUint64;
  typedef GoInt64 GoInt;
  typedef GoUint64 GoUint;
  typedef __SIZE_TYPE__ GoUintptr;
  typedef float GoFloat32;
  typedef double GoFloat64;
  typedef float _Complex GoComplex64;
  typedef double _Complex GoComplex128;
  
  /*
    static assertion to make sure the file is being used on architecture
    at least with matching size of GoInt.
  */
  typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
  
  typedef struct { const char *p; GoInt n; } GoString;
  typedef void *GoMap;
  typedef void *GoChan;
  typedef struct { void *t; void *v; } GoInterface;
  typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
  
  #endif
  
  /* End of boilerplate cgo prologue.  */
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  
  extern GoUint64 Add(GoUint64 p0, GoUint64 p1);
  
  #ifdef __cplusplus
  }
  #endif
  </code></pre>
  
  <p>but this command:  </p>
  
  <pre><code>gcc -o test _main.c libgolang.a -lpthread
  </code></pre>
  
  <p>output:  </p>
  
  <pre><code>libgolang.a(go.o):(.data+0x2150): undefined reference to `NtWaitForSingleObject'
  libgolang.a(go.o):(.data+0x21b8): undefined reference to `WSAGetOverlappedResult'
  libgolang.a(go.o):(.data+0x21d8): undefined reference to `timeBeginPeriod'
  collect2.exe: error: ld returned 1 exit status
  </code></pre>
  
  <p><code>go version go1.7rc3 windows/amd64</code>  </p>
  
  <p>I want to use this <code>libgolang.a</code> from  C code to build C executable: <code>test.exe</code></p>
  
  <p>any workaround?</p>
  
  <p>some useful links:  </p>
  
  <p><a href="http://blog.ralch.com/tutorial/golang-sharing-libraries/" rel="nofollow noreferrer">http://blog.ralch.com/tutorial/golang-sharing-libraries/</a><br>
  <a href="https://stackoverflow.com/questions/32215509/using-go-code-in-an-existing-c-project">Using Go code in an existing C project</a><br>
  <a href="https://stackoverflow.com/questions/30072281/using-go-on-existing-c-project">Using Go on existing C project</a>  </p>