My directory structure is as below.
> ├── pkg
> └── src
> └── util
All my source code is present in /src folder, here
> main.go --(Invokes)->cmain.c---(invokes)-->main.go
My main.go file is shown below
> package main
> import (
> /*
> #include "cmain.h"
> */
> "C"
> "os"
> "util"`
> "unsafe"
> )
> func main() {
> argc := C.int(len(os.Args))
> argv := make([]*C.char, argc)
> for i, arg := range os.Args {
> argv[i] = C.CString(arg)
> }
> C.cmain(argc, (**C.char)(unsafe.Pointer(&argv[0])))
> }
> func sample_f(filename *C.char) int {
> cs := C.GoString(filename)
> util.Validation(cs)
> util.XML_Read(cs)
> return 0
> }
My cmain.c file is as shown below
> #include <stdio.h>
> #include "_cgo_export.h"
> void cmain(int argc, char** argv) {
> if( argc == 2 ){
> sample_f(argv[1]);
> }
> }
NOTE: I have all the required .h for .c files
The sample_f() function is present in sample.go, for which i am trying to create a separate package called “util” in /src,
in sample.go
(path shown below), i have included package util
at the top of file.
├── cmain.c
├── cmain.h
├── main.go
├── makefile
└── util
└── sample.go
When ever i do a make i always get below error.
> env: tool: No such file or directory
> make: *** [_cgo_export.c] Error 127
My makefile is shown below,
> GOROOT=/usr/local/go
> GO=/usr/local/go/bin/go
> GOPATH=$(shell pwd)
> PKG_DIR=`pwd`/../pkg/linux_amd64
> GO_TOOL_FLAGS=-v -D ${PKG_DIR}
> CC=gcc
> CGO_SRCS= main.go util/sample.go
> GENFILES_GO=_cgo_gotypes.go \
> $(CGO_SRCS:.go=.cgo1.go)
> GENFILES_C=_cgo_export.c \
> $(CGO_SRCS:.go=.cgo2.c)
> EXPORT_H=_cgo_export.h
> GENFILES= $(GENFILES_GO) \
> $(GENFILES_C) \
> $(EXPORT_H) \
> _cgo_main.c \
> _cgo_flags
> all: _cgo_.o libSample.a SAMPLE
> .c.o:
> ${CC} -c -o $@ -fPIC $<
> clean:
> rm -f $(GENFILES) *.o libSample.a ../bin/SAMPLE _cgo_import.go
> $(GENFILES): $(CGO_SRCS) cmain.h
> env CGO_LDFLAGS=${GO} tool cgo -objdir . \
> -importpath $(CGO_SRCS)
> rm _cgo_.o
> cmain.o: cmain.c cmain.h
> _cgo_.o: $(GENFILES_C:.c=.o) $(EXPORT_H) _cgo_main.o cmain.o
> ${CC} -o _cgo_.o $(GENFILES_C:.c=.o) _cgo_main.o cmain.o
> _all.o: $(GENFILES_C:.c=.o) $(EXPORT_H) cmain.o
> ${CC} -fPIC -nostdlib -o _all.o -Wl,-r $(GENFILES_C:.c=.o) cmain.o
> _cgo_import.go: _cgo_.o
> ${GO} tool cgo -dynimport _cgo_.o -dynout _cgo_import.go \
> -dynpackage main util
> libSample.a: $(GENFILES_GO) _cgo_import.go main.go _all.o
> ${GO} tool compile ${GO_TOOL_FLAGS} -pack -o libSample.a $(GENFILES_GO) _cgo_import.go
> ${GO} tool pack r libSample.a _all.o
> SAMPLE: libSample.a
> /usr/local/go/pkg/tool/linux_amd64/link -L ${PKG_DIR} -buildmode exe -o ../bin/SAMPLE libSample.a
Can anyone give some pointers as to how to resolve this issue.