title: Can Cgo call C function declared in another directory?
body: <p>It seems that I can't use Cgo to call a C function declared in another directory rather than current Go package. </p>
  
  <p>Codes of all files :</p>
  
  <pre><code>// TestGoCallOC.go
  package main
  /*
  #include "test.h"
  #cgo CFLAGS: -x objective-c
  #cgo LDFLAGS: -framework Foundation
  */
  import "C"
  import (
      "fmt"
  )
  func main()  {
      fmt.Println(C.fortytwo())
  }
  
  // test.h
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  int fortytwo();
  
  // test.m
  #include "test.h"
  int fortytwo() {
      return 42;
  }
  </code></pre>
  
  <p>If I put all the files in one directory:</p>
  
  <pre><code>|--src
     |--TestGoCallOC
        |--TestGoCallOC.go
        |--test.h
        |--test.m
  </code></pre>
  
  <p>and run the Go main function, these two C functions could be called.</p>
  
  <p>But if I put C files (actually they are Objective-C files) in another direcory:</p>
  
  <pre><code>|--src
     |--TestGoCallOC
        |--TestGoCallOC.go
     |--SomeOCCodes
        |--test.h
        |--test.m
  </code></pre>
  
  <p>, change the file path of <code>#include "test.h"</code> in preamble to it absolute path, and run the Go main function, these two C functions could not be called.</p>
  
  <p>Error message is </p>
  
  <p>Undefined symbols for architecture x86_64:
    "_fortytwo", referenced from:
        __cgo_b3674ecbf56b_Cfunc_fortytwo in TestGoCallOC.cgo2.o
       (maybe you meant: __cgo_b3674ecbf56b_Cfunc_fortytwo)
  ld: symbol(s) not found for architecture x86_64
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  exit status 2</p>
  
  <p>Did I do something wrong? Or is Cgo not capable of doing this?</p>