8

I'm trying to compile a go project in a raspberry pi.

The project has 5 files, two small .c files and its counterparts .h (one of these files is my code -- it calls the other, which is a base64 library) and a .go files which calls my .c code using cgo.

When I compile my C code only (with its calls and everything) with gcc alone at the raspberry pi it does well without any configuration.

When I compile the entire go project on my x86 Linux Ubuntu machine with go build, it also does pretty well.

But when I try to compile the go project with go build in the raspberry pi it doesn't 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 the <stdlib.h> before the <math.h> the problem occurs for it too, so the problem is not the absence of math.h, I think) I tried to:

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

I don't understand why go is trying to compile base64.c with -I /usr/lib/go/pkg/linux_arm. Really don't. Someone help.

EDIT: Clarifying note about the structure of the project:

It has 5 files, 2 C (and its counterparts 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 should I change in this declarations for this thing to work? And why did it worked on my x86 linux?

fiatjaf
  • 11,479
  • 5
  • 56
  • 72
  • Is this question missing any information? What's the correct/default way of writing and compiling cgo packages? – fiatjaf Oct 05 '13 at 22:08
  • I'll try to put the `base64.c` functions inside the `project.c` file. – fiatjaf Oct 06 '13 at 14:43
  • Didn't work. Now, with only `project.c`, `project.h` and `project.go` it has problems with `project.c` compilation, saying he can't find the file `stdlib.h`. – fiatjaf Oct 06 '13 at 14:55

2 Answers2

3

cgo inline syntax

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

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

without the whitespace between # and cgo. See cmd/cgo for details on the syntax.

-ldflags parameter

The go -ldflags parameter passes parameters to the go linkers (5l, 6l, 8l, ...). Even if the parameter would be passed to the C linker, this wouldn't do you any good as the linker does not handle includes, the compiler does.

I'm afraid that this parameter won't help you here. All relevant parameters should be configured in the go source file using the #cgo tags.

misc. notes

If you're using math.h you most likely need to link libmath. You can do this by writing this to your go source file:

// #cgo LDFLAGS: -lm
nemo
  • 55,207
  • 13
  • 135
  • 135
  • Thanks, but I was already using the correct `#cgo` syntax, I just mistyped when writing the question (I will edit it), and the `-lm` flag did not work either. The problem is that `` is the first header file called, if I change its place with `` it doesn't find it the same way. – fiatjaf Oct 06 '13 at 03:34
  • In your update you still use `LDFLAGS` for `-I`. Use the `CFLAGS` parameter. – nemo Oct 06 '13 at 12:50
  • Yes, that was a true mistake of mine. Thank you. But I have tried with all the syntaxes and it doesn't make a difference in the output. I corrected it again now. – fiatjaf Oct 06 '13 at 14:38
1

It seems my problem was something related to not having set the CGO_ENABLED flag.

I don't know for sure, but it seems, because I uninstalled my Go from the Raspbian repositories (which seems to come with CGO disabled by default) and installed Go from source (just like I had made in my x86 Linux) then it started to work.

fiatjaf
  • 11,479
  • 5
  • 56
  • 72