main.go
package main
/*
#cgo CFLAGS: -g -Wall
#include <stdlib.h>
#include "greeter.h"
*/
import "C"
import (
"unsafe"
)
func main() {
name := C.CString("Gopher")
defer C.free(unsafe.Pointer(name))
year := C.int(2018)
C.greet(name, year)
}
greeter.h
#ifndef _GREETER_H
#define _GREETER_H
void greet(const char *name, int year);
#endif
greeter.c
#include "greeter.h"
#include <stdio.h>
void greet(const char *name, int year) {
printf("Greetings, %s from %d! We come in peace :)", name, year);
}
cmd
$ go build -buildmode=c-archive -o greeter.a main.go
//output
# command-line-arguments
Undefined symbols for architecture x86_64:
"_greet", referenced from:
__cgo_4d6b7d3a5d3b_Cfunc_greet in _x002.o
(maybe you meant: __cgo_4d6b7d3a5d3b_Cfunc_greet)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Have you ever solve this, could you give me any advice?
Thanks,