Go build does not find my standard C library when compiling cgo package

I am trying to compile go project in raspberry pi.

There are 5 files in the project, two small .c files and its associations .h (one of these files is my code - it calls the other, which is the base of the .go ) and .go files that call my .c with cgo .

When I compile only my C code (with my calls and everything) with gcc only in raspberry pi, it does fine without any configuration.

When I compile the whole go project on my x86 Linux Ubuntu computer with go build , it also works very well.

But when I try to compile the go project with go build in raspberry pi, it does not get my C libraries:

 fiatjaf@raspberrypi ~/g/s/b/f/project> go build -x WORK=/tmp/go-build702187084 mkdir -p $WORK/bitbucket.org/fiatjaf/project/_obj/ cd /home/fiatjaf/go/src/bitbucket.org/fiatjaf/project /usr/lib/go/pkg/tool/linux_arm/5c -FVw -I $WORK/bitbucket.org/fiatjaf/project/_obj/ -I /usr/lib/go/pkg/linux_arm -o $WORK/bitbucket.org/fiatjaf/project/_obj/base64.5 -DGOOS_linux -DGOARCH_arm ./base64.c # bitbucket.org/fiatjaf/project ./base64.c:2 5c: No such file or directory: math.h 

(If I put <stdlib.h> in front of <math.h> , the problem also arises for him, so the problem is not the absence of math.h, I think) I tried:

  • add // #cgo CFLAGS: -I/usr/include to the .go file
  • add // #cgo LDFLAGS: -I/usr/include (I cannot find out what the proper use of these flags is)
  • use go build -ldflags '-I/usr/include'

I do not understand why go is trying to compile base64.c with -I /usr/lib/go/pkg/linux_arm . Not really. Someone will help.

EDIT: Clarification Note on Project Structure:

It has 5 files, 2 C (and its mapping H):

base64.c

 #include <math.h> #include <stdint.h> #include <stdlib.h> ... // definitions of functions used at project.c 

project.c

 #include <stdlib.h> #include <string.h> #include "base64.h" ... // functions used at project.go 

and 1 Go:

 ... // #include <stdlib.h> // #include <string.h> // #include "project.h" // #cgo CFLAGS: -I/usr/include // #cgo LDFLAGS: -lm import "C" ... 

Where, what, and how do I change these declarations to make this thing work? And why did this work for my x86-Linux?

+7
arm go raspberry-pi cgo
source share
2 answers

My problem seems to be that the CGO_ENABLED flag is not set.

I don’t know for sure, but it seems because I removed my Go from the Raspbian repositories (which CGO apparently turned off by default) and installed Go from source (as in my x86 Linux), then it started working.

+1
source share

cgo built-in syntax

The correct syntax for cgo parameters in the go file is:

 // #cgo CFLAGS: -I/usr/include 

without spaces between # and cgo . For syntax details see cmd / cgo .

-ldflags option

The go parameter -ldflags passes parameters to the go linkers (5l, 6l, 8l, ...). Even if the parameter is passed to the C C-linker, it will not do you any good since the linker does not handle include, the compiler does.

I am afraid that this option will not help you. All relevant parameters must be configured in the original source file using the #cgo tags.

Other. Notes

If you use math.h , you will most likely need to bind libmath. You can do this by writing this to the go source file:

 // #cgo LDFLAGS: -lm 
+3
source share

All Articles