gccgo build can not find custom pakage

178 views Asked by At

when use the gccgo build one single go file is ok, but whn I build a large multi custom pacage go mod project, the project have some sub package(such as app/ app/core/server etc) not build success.

how to fix this? anyhelp? I build with -x and see some debug info.

go build -x  -gccgoflags -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go

the error is:

WORK=/tmp/go-build1609005358
mkdir -p $WORK/b001/
cd $WORK
/opt/gccgo/bin/gccgo -fgo-importcfg=/dev/null -c -x c - -o /dev/null || true
mkdir -p $WORK/b001/_importcfgroot_/github.com/gin-gonic
ln -s /home/liangqi1/.cache/go-build/8a/8ae3d6bb3097698b7be6547599d7a61c9c5c54cca0be55c007ca1c8386d1188c-d $WORK/b001/_importcfgroot_/github.com/gin-gonic/libgin.a
mkdir -p $WORK/b001/_importcfgroot_/github.com/logrusorgru
ln -s /home/liangqi1/.cache/go-build/6f/6fb602f3c990310188a3e921909d152810f07d50f1ab4c621ab71b87afd4942d-d $WORK/b001/_importcfgroot_/github.com/logrusorgru/libaurora.a
mkdir -p $WORK/b001/_importcfgroot_/go.uber.org
ln -s /home/liangqi1/.cache/go-build/c0/c0ac9f2f0ebb74e0997bfa1d72d21a79ce906ed736197f83402365203094c3a9-d $WORK/b001/_importcfgroot_/go.uber.org/libautomaxprocs.a
/opt/gccgo/bin/gccgo -ffile-prefix-map=a=b -c -x c - -o /dev/null || true
cd /home/liangqi1/gccgo_demo
/opt/gccgo/bin/gccgo -c -g -m64 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/liangqi1/gccgo_demo -o $WORK/b001/_go_.o -I $WORK/b001/_importcfgroot_ -Wl,-R,/opt/gccgo/lib64/ ./main.go
# command-line-arguments
./main.go:7:23: 错误:import file ‘gccgo_demo/app’ not found
    7 |         "gccgo_demo/app"
      |                       ^
./main.go:8:35: 错误:import file ‘gccgo_demo/app/core/server’ not found
    8 |         "gccgo_demo/app/core/server"
      |                                   ^
./main.go:24:19: 错误:reference to undefined name ‘app’
   24 |         if err := app.Init(prjHome); err != nil {
      |                   ^
./main.go:29:12: 错误:reference to undefined name ‘app’
   29 |         if app.IsProd() {
      |            ^
./main.go:35:9: 错误:reference to undefined name ‘server’
   35 |         server.Run()
      |         ^

the gccgo just clone from gcc git:

gccgo (GCC) 12.0.1 20220217

go version is 1.16.4

my project struct like flow:

gccgo_demo
├── app
│   ├── app.go
│   ...
├── go.mod
├── go.sum
├── main.go
 ... other code

the go mod name is gccgo_demo so , the app/app.go has some import like gccgo_demo/app path.

1

There are 1 answers

1
isavinof On

I don't see your code but I guess the problem that you do not pass the include path.

The cgo tool will always invoke the C compiler with the source file's directory in the include path; i.e. -I${SRCDIR} is always implied. This means that if a header file foo/bar.h exists both in the source directory and also in the system include directory (or some other place specified by a -I flag), then "#include <foo/bar.h>" will always find the local version in preference to any other version.

So just do smth like

go build -x  -gccgoflags -I/path/to/your/file -Wl,-R,/opt/gccgo/lib64/ -compiler gccgo -o bin/app main.go

More details https://pkg.go.dev/cmd/cgo