CGO did not generate header file at Mac OS - go

I run the below code at Win 10 and got both lib and lib.h generated, tried the same at Mac but only lib got generated, and the lib.h not appeared!!
// file: lib/go.mod
module hasan/lib
go 1.14
And
// file: lib/main.go
package main
import "fmt"
//export HelloWorld
func HelloWorld() {
fmt.Printf("hello world")
}
func main() {}
And made the build as:
$ go build -buildmode=c-shared
No error show up

Th error was due to missing:
import "C"
In the lib/main.go this is the one responsible about generating the proper header file lib.h
Example
Create go file lib.go
package main
import "C"
import "fmt"
//export HelloWorld
func HelloWorld() {
fmt.Printf("hello world from GO\n")
}
func main() {}
Compile the above as:
go build -o lib.so -buildmode=c-shared lib.go
Then app.c as:
#include<stdio.h>
#include "lib.h"
int main(){
HelloWorld();
printf("Hello from c\n");
return 0;
}
And compile it (and build executable) as:
cc -o output app.c lib.so
And call the output as:
Hasans-Air:Documents hajsf$ ./output
hello world from GO
Hello from c

Related

gccgo-cross-compile can't compile cgo

I am trying to compile a simple go program using gccgo. My code uses cgo thou, gccgo couldn't compile it. This is my code (which compiles using go compiler):
package main
// #include <stdio.h>
// #include <stdlib.h>
//
// static void myprint(char* s) {
// printf("%s\n", s);
// }
import "C"
import "fmt"
func main(){
fmt.Println("Start")
cs := C.CString("Print from C")
C.myprint(cs)
fmt.Println("End")
}
when I compile the code using gccgo main.go -o gccoutput I get:
main.go:9:9: error: import file ‘C’ not found
import "C"
^
main.go:13:8: error: reference to undefined name ‘C’
cs := C.CString("Print from C")
^
main.go:14:2: error: reference to undefined name ‘C’
C.myprint(cs)
^
any ideas how to solve this?
EDIT:
I am trying to compile to ppc using the gccgo, and I don't want to use cross-compilation process of go compiler. I have tried to do (as suggested in the comments):
go build -compiler=gccgo
and it worked. Thou, when I do:
go build -comiler=powerpc-linux-gnu-gccgo main.go
I get:
invalid value "powerpc-linux-gnu-gccgo" for flag -compiler: unknown compiler "powerpc-linux-gnu-gccgo"
usage: go build [-o output] [-i] [build flags] [packages]
Run 'go help build' for details.

Problems with import "C"

I'm trying to get GO to compile a simple test script on Windows 10 using GoLand but am running into problems. Here is the code:
package main
import "fmt"
import "C"
import (
"math"
)
func main() {
fmt.Println("working")
}
//export add
func add( a , b int) int {
return a + b
}
//export Cosine
func Cosine(x float64) float64 {
return math.Cos(x)
}
When I comment out the import "C" line the code compiles fine but when its there I get
exec: "gcc": executable file not found in %PATH%
So I installed MinGW and added its bin to the PATH variable so that in a cmd prompt I can run
C:\GolandProjects\LearnGoProject>gcc
gcc: fatal error: no input files
compilation terminated.
However I'm still getting the error. Could anybody suggest how to fix the issue?

`go build` versus `go build file.go`

I am having trouble building a very simple go program that calls c code via cgo.
My setup:
$: echo $GOPATH
/go
$: pwd
/go/src/main
$: ls
ctest.c ctest.h test.go
test.go contains:
package main
// #include "ctest.c"
// #include <stdlib.h>
import "C"
import "unsafe"
import "fmt"
func main() {
cs := C.ctest(C.CString("c function"))
defer C.free(unsafe.Pointer(cs))
index := "hello from go: " + C.GoString(cs)
fmt.Println(index)
}
ctest.h contains:
char* ctest (char*);
ctest.c contains:
#include "ctest.h"
char* ctest (char* input) {
return input;
};
When I run go build test.go I get a binary, test that I can run which prints the expected hello from go: c function
However when I run go build I get the error:
# main
/tmp/go-build599750908/main/_obj/ctest.o: In function `ctest':
./ctest.c:3: multiple definition of `ctest'
/tmp/go-build599750908/main/_obj/test.cgo2.o:/go/src/main/ctest.c:3: first defined here
collect2: error: ld returned 1 exit status
What is happening with go build that is not in go build test.go that is causing the error?
Read your code carefully. Read the error message. Correct your error:
// #include "ctest.h"
test.go:
package main
// #include "ctest.h"
// #include <stdlib.h>
import "C"
import "unsafe"
import "fmt"
func main() {
cs := C.ctest(C.CString("c function"))
defer C.free(unsafe.Pointer(cs))
index := "hello from go: " + C.GoString(cs)
fmt.Println(index)
}
ctest.h:
char* ctest (char*);
ctest.c:
#include "ctest.h"
char* ctest (char* input) {
return input;
};
Output:
$ rm ./test
$ ls
ctest.c ctest.h test.go
$ go build
$ ls
ctest.c ctest.h test test.go
$ ./test
hello from go: c function
$

Go build shared-c is not outputting a header file

I am trying to test build a shared C lib in GoLang, and the output does not create a header file (.h)
test.go:
package main
import "C"
import "fmt"
func ExportedFun(s string) {
fmt.Printf("C gave us %s string", s)
}
func main() {}
and the command I run is:
go build -buildmode=c-shared -o test.so test.go
I get the .so file but no header file. Is there something I am missing?
From the go command documentation:
The only callable symbols will be those functions exported using a cgo
//export comment.
Th syntax for exporting a function via cgo can be found in the cgo documentation
Go functions can be exported for use by C code in the following way:
//export MyFunction
func MyFunction(arg1, arg2 int, arg3 string) int64 {...}
//export MyFunction2
func MyFunction2(arg1, arg2 int, arg3 string) (int64, *C.char) {...}
Marking your function as exported will generate the header.

GDB can't debug the go program within cgo code

example files
src/test.go
package main
import (
. "clib"
)
func main() {
a := "123";
b := "456";
c := "789";
println(a,b,c);
Output("ABC");
}
src/clib/clib.h
#ifndef CLIB
void output(char* str);
#endif
src/clib/clib.c
#include "clib.h"
#include <stdio.h>
void output(char* str)
{
printf("%s\n", str);
}
src/clib/clib.go
package clib
/*
#cgo CFLAGS:-g
#include "clib.h"
*/
import "C"
func Output(s string) {
p := C.CString(s);
C.output(p);
}
exec code
go build -gcflags "-N -l" test.go
gdb ./test
b 10
r
info locals // <- every variable's value is wrong!
Who can help me solve this problem, thank you very much.
My Environment:
ubuntu 11.04 i386
gdb 7.6
go 1.1
There is currently an open bug regarding this: https://code.google.com/p/go/issues/detail?id=5221
Debugging cgo with gdb worked in 1.0 but is currently broken in 1.1. It's being worked on.

Resources