It seems that I can't use Cgo to call a C function declared in another directory rather than current Go package.
Codes of all files :
// 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 <stdio.h>
#include <stdlib.h>
int fortytwo();
// test.m
#include "test.h"
int fortytwo() {
return 42;
}
If I put all the files in one directory:
|--src
|--TestGoCallOC
|--TestGoCallOC.go
|--test.h
|--test.m
and run the Go main function, these two C functions could be called.
But if I put C files (actually they are Objective-C files) in another direcory:
|--src
|--TestGoCallOC
|--TestGoCallOC.go
|--SomeOCCodes
|--test.h
|--test.m
, change the file path of #include "test.h"
in preamble to it absolute path, and run the Go main function, these two C functions could not be called.
Error message is
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
Did I do something wrong? Or is Cgo not capable of doing this?